Reactivecocoa Introduction I will not say, can own Baidu. Reactivecocoa This framework is now very popular, but not popular. In the final analysis, is the study cost is too high, the light to see the document, you almost do not know how to use. Reactivecocoa internal implementation of a large number of blocks, in the case of block nesting deeper, read the source code also become difficult. I'm going to give you a pull-off and break down each module. Because when I first started reading, I didn't even know how to start. Source code Analysis If there are errors, please correct me.
First, I'll show you a Reactivecocoa code.
```
RACSignal *signal = [RACSignal createSignal:^(id subscriber) { [subscriber sendNext:[NSDate date]]; [subscriber sendCompleted]; returnnil; }]; [signal subscribeNext:^(id x) { NSLog(@"next:%@", x); } error:^(NSError *error) { NSLog(@"error:%@", error); } completed:^{ NSLog(@"completed"); }];The function of this code is to create a model and subscribe to the model. The code is elegant, isn't it? The elegance behind, always need a lot of work to support. Now I'm going to rewrite a RAC, but it's a simple version, and the simple version is to keep the main logic and the details to be processed. This section only implements the above features. Two classes of qhqsignal and Qhqsubscriber "are implemented here
@interface qhqsignal ()
@property (nonatomic, copy) void (^didsubscriber) (ID subscriber);
@property (nonatomic, strong) Qhqsubscriber *scriber;
@end
@implementation qhqsignal
+ (Qhqsignal *) Createsignal: (void (^) (ID subscriber)) didsubscriber {
Qhqsignal *signal = [[Self alloc] init];
Signal.didsubscriber = Didsubscriber;
return signal;
}
-(void) Subscribenext: (void (^) (id)) Nextblock {
Qhqsubscriber *scriber = [Qhqsubscriber subscriberwithnextblock:nextblock];
Self.scriber = Scriber;
Self.didsubscriber (Self.scriber);
}
@end
``````
@interface Qhqsubscriber ()
@property (nonatomic, copy) void (^nextblock) (id x);
@property (nonatomic, copy) void (^completionblock) (id x);
@property (nonatomic, copy) void (^errorblock) (id x);
@end
@implementation Qhqsubscriber
+ (Instancetype) Subscriberwithnextblock: (void (^) (id x)) nextblock {
return [self Subscriberwithnextblock:nextblock completionblock:nil errorblock:nil];
}
+ (Instancetype) Subscriberwithnextblock: (void (^) (id x)) nextblock Completionblock: (void (^) (id x)) completionblock Errorblock: (void (^) (id x)) errorblock {
Qhqsubscriber *subscriber = [[Qhqsubscriber alloc] init];
Subscriber.nextblock = Nextblock;
Subscriber.completionblock = Completionblock;
Subscriber.errorblock = Errorblock;
return subscriber;
}
-(void) Sendnext: (id) Next {
if (Self.nextblock) {
Self.nextblock (next);
}
}
@end
The qhqsignal implements a creation method that requires the block to be executed when the subscription is created, and the block has a subcriber parameter, which is the internal subscriber of the signal (the elegance of the RAC implementation, the first confusing design), This subscriber saves a real subscriber, in the process of subscribing to the signal. For example, you tell this subcriber what to do when the signal comes. Once the signal has been created, the RAC creates a subscriber internally once the Subscriber, that is, the signal is subscribenext (for the sake of temporarily not considering Compelete, and error). And this subscriber saved the block of your execution. Subscriber is actually equivalent to getting the authorization. ```
Qhqsignal *demosignal = [qhqsignal createsignal:^ (ID subscriber) {
[Subscriber sendnext:@ "1"];
}];
[Demosignal subscribenext:^ (ID x) {
NSLog (@ "%@", X);
}];
The simple implementation of the above code, is not much simpler. A section is written more, it is easy to spit, and see it.
Analysis of Reactivecocoa source code splitting (i.)