gpt4 book ai didi

ios - Reactive Cocoa 中的可取消超时

转载 作者:可可西里 更新时间:2023-11-01 05:23:16 24 4
gpt4 key购买 nike

我想使用 Reactive Cocoa 实现倒数计时器在 iOS 中。定时器应该运行 X 秒并且每秒做一些事情。我无法弄清楚的部分是我可以取消超时的方式。

RACSubscribable *oneSecGenerator = [RACSubscribable interval:1.0];
RACDisposable *timer = [[oneSecGenerator take:5] subscribeNext:^(id x) {
NSLog(@"Tick");
}];

最佳答案

我想,我找到了解决方案。诀窍是将取消信号合并到滴答信号中,然后取 X 个样本。最终订阅者将在每次 tick 信号滴答时收到一个 next 事件,并在“take”完成时收到 completed 事件。可以通过在取消计时器上发送错误来实现取消。

__block RACSubject *cancelTimer = [RACSubject subject];
RACSubscribable *tickWithCancel = [[RACSubscribable interval:1.0] merge:cancelTimer];
RACSubscribable *timeoutFiveSec = [tickWithCancel take:5];

[timeoutFiveSec subscribeNext:^(id x) {
NSLog(@"Tick");
} error:^(NSError *error) {
NSLog(@"Cancelled");
} completed:^{
NSLog(@"Completed");
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}];

要激活取消,必须执行以下操作。

[cancelTimer sendError:nil]; // nil or NSError

关于ios - Reactive Cocoa 中的可取消超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13555750/

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