Managing Nested Asynchronous Callbacks in Objective-C using ReactiveCocoa by AFNetworking
Use of the following Cocoapods is required:
- Download the project.
- Add
RRApiManager.h
&RRApiManager.m
with necessary files in your project. - Congratulations!
A RACSignal should wrap an operation with an asynchronous callback block.
The last signal to run should call subscribeNext for receiving output values. If no output values are available, subscribeNext can be replaced by subscribeCompleted or subscribeError.
Any intermediate step is defined by flattenMap. The parameter in flattenMap corresponds to the data from the next signal. It also takes a return value, which is the subsequent signal to run. The two signals are written top-down and are executed top-down.
We don’t want to strongly retain self: in ARC, we introduce __weak. Also, the extra square brackets at start and end are ugly in Objective-C’s implementation of functional reactive programming.
@weakify(self);
NSDictionary *param = @{@"key": @"test"};
[[[[[RRApiManagerShared postAPICallURL:API_URL header:nil param:param]
flattenMap:^(id responseOfPostAPI) {
NSLog(@"%@",responseOfPostAPI);
return [RRApiManagerShared getAPICallURL:API_URL header:nil];
}]
subscribeOnBackgroundAndDeliverOnMainThread]
subscribeNext:^(id responseGetAPI) {
NSLog(@"%@",responseGetAPI);
} error:^(NSError * error) {
NSLog(@"%@",error.localizedDescription);
}] rac_deallocDisposable];
To run the project, clone the repo, and run pod install from the RRReactiveNetworking directory first.
We would love you for the contribution to RRReactiveNetworking, check the LICENSE
file for more info.
RRReactiveNetworking is available under the MIT license. See the LICENSE file for more info.