Skip to content

Instantly share code, notes, and snippets.

@hsavit1
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsavit1/cb6d891dfdb8e38d518e to your computer and use it in GitHub Desktop.
Save hsavit1/cb6d891dfdb8e38d518e to your computer and use it in GitHub Desktop.
//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