Last active
August 29, 2015 14:23
-
-
Save hsavit1/cb6d891dfdb8e38d518e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the trick is to use replayLast or replayLazily signal operation, which basically creates buffer and hold in it cached value! | |
- (void)getData:(id)inputParam forKey:(NSString *)key withCompletionBlock:(void (^)(id data, NSError *error)) { | |
if (!_cache[key]) { | |
RACSignal *dataSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { | |
id operation = _actualGetDataRequest(inputParam, ^(id data, NSError *error) { | |
if (error) { | |
[subscriber sendError:error]; | |
} | |
else { | |
[subscriber sendNext:asyncActionResult]; | |
[subscriber sendCompleted]; | |
} | |
}); | |
return [RACDisposable disposableWithBlock:^{ | |
[operation cancel]; | |
}]; | |
}]; | |
_cache[key] = dataSignal.replayLast; | |
} | |
if (completionBlock) { | |
RACSignal *cachedSignal = _cache[key]; | |
[cachedSignal subscribeNext:^(id data){ | |
completionBlock:(data, nil); | |
} error: ^(NSError *error) { | |
completioBlock:(nil, error); | |
}] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment