Question about cache - network data gathering #1166

bolismauro opened this Issue Mar 11, 2014 · 4 comments


None yet

3 participants


Hi everyone,
i'm new to reactivecocoa but it seems a really interesting library so i decide to use it in my new application.
Here is my question: i have some pieces of information that are gathered from a remote web service. Every time the application starts i have to check if there are new information. It the version of the online information is different than the local one i have to download them, otherwise i can use the local cached.
At the end i have to populate a table.

I don't understand how to conditionally execute a method (ie: check version -> if needed download info from remote web service and save them -> gather local info -> populate UI).

It is also possible to execute this flow every X seconds?

Thank you


Here's a theoretical example. Hopefully it gives you a sense of the shape of the code you'd need to write.

- (RACSignal *)loadData {
    return [[RACSignal 
        createSignal:^(id<RACSubscriber> subscriber) {
            // If the cache is valid then we can just immediately send the 
            // cached data and be done.
            if (self.cacheValid) {
                [subscriber sendNext:self.cachedData];
                [subscriber sendCompleted];
            } else {
                [subscriber sendError:self.staleCacheError];
        // Do the subscription work on some random scheduler, off the main 
        // thread.
        subscribeOn:[RACScheduler scheduler]];

- (void)update {
        // Catch the error from -loadData. It means our cache is stale. Update
        // our cache and save it.
        catch:^(NSError *error) {
            return [[self updateCachedData] doNext:^(id data) {
                [self cacheData:data];
        // Our work up until now has been on a background scheduler. Get our 
        // results delivered on the main thread so we can do UI work.
        subscribeNext:^(id data) {
            // Update your UI based on `data`.

            // Update again after `updateInterval` seconds have passed.
            [[RACSignal interval:updateInterval] take:1] subscribeNext:^(id _) {
                [self update];

Let me know if you have specific questions, or need me to explain some part of that.


Just the last question. If i want to tigger this every X seconds i have to create the signal in loadData with [RACSignal interval:X]
Thank you for your help!


Sure, that's one way to do it. As it is above, -update would get called every X seconds, which would also call -loadData.


Good, thank you very much!

@joshaber joshaber closed this Mar 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment