gpt4 book ai didi

swift - RxSwift 缓冲区性能

转载 作者:行者123 更新时间:2023-11-30 11:18:52 26 4
gpt4 key购买 nike

是否存在一种不使用计时器连续轮询的缓冲区?因此,性能确实受到了影响。理想情况下,buffer 在生成值之前会等待 n 秒,然后在接收到新事件之前不会设置新计时器。

最佳答案

这是我对非轮询缓冲区的尝试:

extension Observable {

private struct Buffered<T> {
let lastFlush: Date
let remaining: RxTimeInterval
let value: [T]
}

func buffered(delay: RxTimeInterval) -> Observable<[Element]> { return
scan(
Buffered<Element>(
lastFlush: Date(),
remaining: delay,
value: []
)
) { sum, x in
let latest = Date()
let isExpired = latest > (sum.lastFlush + delay)
let adjusted = isExpired ? latest : sum.lastFlush
return Buffered<Element>(
lastFlush: adjusted,
remaining: isExpired
? delay
: delay - latest.timeIntervalSince(adjusted)
,
value: isExpired ? [x] : sum.value + [x]
)
}
.flatMapLatest {
Observable
.just($0.value)
.delay(
$0.remaining,
scheduler: MainScheduler.instance
)
//.ignoringCompletions()
}
}

public func ignoringCompletions() -> Observable { return
concat(Observable.never())
}
}

关于swift - RxSwift 缓冲区性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51503551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com