简体   繁体   中英

ReactiveCocoa recurring signal

I have the following code:

- (RACSignal *)addSyncTask:(RACSignal *)task withInterval:(NSTimeInterval)interval
{
    return [[task concat:[[RACSignal empty] delay:interval]] then:^RACSignal *{
        return [self addSyncTask:task withInterval:interval];
    }];
}

- (void)setupFooSync
{
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [self.API syncFooWithCompletion:^(id response, NSError *error) {
            [subscriber sendError:error];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
    [self addSyncTask:signal withInterval:60];
}

The idea is that once setupFooSync is invoked, the sync will occur once every 60 seconds until the containing instance is deallocated. However, with the code above, the sync never occurs. When I change +createSignal: to +startLazilyWithScheduler: , it occurs only once. I am guessing that since the same sync signal is being reused every time, there needs to be some sort of reset. Any pointers would be greatly appreciated.

It seems that concat doesn't trigger the needed subscribe method, try this instead:

[[[task then:^RACSignal *{
    return [[RACSignal interval:5 onScheduler:[RACScheduler mainThreadScheduler]] take:1];
}] repeat] subscribeCompleted:^{

}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM