gpt4 book ai didi

ios - 重置定时器信号的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-29 10:26:14 44 4
gpt4 key购买 nike

我需要每 5 分钟从服务器获取一次数据。如果我确实下拉刷新,还需要从服务器获取数据,并重置计时器

下面的代码是现在的解决方案,看起来工作正常。只是想知道如何简化代码? ReactiveCocoa 中可能有更好的方法?

    RACSignal* refreshSignal = [self.refreshControl rac_signalForControlEvents:UIControlEventValueChanged];
self.timerSignal = [[RACSignal interval:300 onScheduler:[RACScheduler scheduler] withLeeway:2] takeUntil:refreshSignal];
[self.timerSignal subscribeNext:^(id x) {
NSLog(@"==========================");
NSLog(@"[Timer1]");
[self.viewModel performFetch];
}];

[refreshSignal subscribeNext:^(id x) {
NSLog(@"==========================");
NSLog(@"[Refresh]");
[self.viewModel performFetch];
self.timerSignal = [[RACSignal interval:300 onScheduler:[RACScheduler scheduler] withLeeway:2] takeUntil:refreshSignal];
[self.timerSignal subscribeNext:^(id x) {
NSLog(@"==========================");
NSLog(@"[Timer2]");
[self.viewModel performFetch];
}];
}];

最佳答案

我能想到的最干净的方法是使用 RACReplaySubject,发送 300 个 interval 信号,然后在每次触发 block 时切换到发送的最新信号.

self.timerSubject = [RACReplaySubject replaySubjectWithCapacity:1];
RACSignal * refreshSignal = [self.refreshControl rac_signalForControlEvents:UIControlEventValueChanged];
RACSignal * timeSignal = [RACSignal interval:300 onScheduler:[RACScheduler scheduler] withLeeway:2];
[self.timerSubject sendNext:timeSignal];

@weakify(self)
[[self.timerSubject.switchToLatest merge:refreshSignal] subscribeNext:^(id _) {
@strongify(self)
[self.viewModel performFetch];
}];

[refreshSignal subscribeNext:^(id _) {
@strongify(self)
[self.timerSubject sendNext:timeSignal];
}];

关于ios - 重置定时器信号的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32131744/

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