So I have just recently started with ReactiveCocoa, and I figured the best way to learn would be just to jump right in and start refactoring some existing code that I have. I was wanting to get some critique and make sure I am heading in the right direction.
So in the app I am refactoring, I have a ton of code that goes like this:
[self.ff getArrayFromUri:@"/States?sort=name asc" onComplete:^(NSError *theErr, id theObj, NSHTTPURLResponse *theResponse) {
if(!theErr) {
//do something with theObj
}
else {
//handle the error
}
}];
I currently have this refactored in ReactiveCocoa like so:
-(void)viewDidLoad {
//ReactiveCocoa
RACCommand *command = [RACCommand command];
RACSubject *subject = [RACSubject subject];
[[[command addSignalBlock:^RACSignal *(id value) {
NSError *err;
NSArray *array = [self.ff getArrayFromUri:@"/States" error:&err];
err ? [subject sendError:err] : [subject sendNext:array];
return [RACSignal empty];
}]switchToLatest]deliverOn:[RACScheduler mainThreadScheduler]];
[subject subscribeNext:^(NSArray *x) {
[self performSegueWithIdentifier:kSomeSegue sender:x];
} error:^(NSError *error) {
NSLog(@"the error = %@", error.localizedDescription);
}];
self.doNotLocation = [UIButton buttonWithType:UIButtonTypeCustom];
[self.doNotLocation setBackgroundImage:[UIImage imageNamed:@"BlackButton_small.png"] forState:UIControlStateNormal];
[[self.doNotLocation rac_signalForControlEvents:UIControlEventTouchUpInside] executeCommand:command];
RAC(self.doNotLocation.enabled) = RACAbleWithStart(command, canExecute);
RAC([UIApplication sharedApplication],networkActivityIndicatorVisible) = RACAbleWithStart(command, executing); }
Is this about how I should be going about it, using the RACSubject, or is there a better way? This whole concept is new to me, as my only programming languages thus far have been Java and Objective-C, so this functional reactive way of thinking is throwing me off a bit.