所以我最近才开始使用 ReactiveCocoa,我认为最好的学习方法就是直接进入并开始重构我拥有的一些现有代码。我想得到一些批评,并确保我朝着正确的方向前进。
所以在我正在重构的应用程序中,我有大量的代码是这样的:
[self.ff getArrayFromUri:@"/States?sort=name asc" onComplete:^(NSError *theErr, id theObj, NSHTTPURLResponse *theResponse) {
if(!theErr) {
//do something with theObj
}
else {
//handle the error
}
}];
我目前在 ReactiveCocoa 中对此进行了重构,如下所示:
-(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); }
这是关于我应该如何处理它,使用 RACSubject,还是有更好的方法?这整个概念对我来说是新的,因为到目前为止我唯一的编程语言是 Java 和 Objective-C,所以这种功能反应式的思维方式让我有点失望。