不用太多浮夸繁雜的介紹婴梧,要的就是會(huì)用下梢。在使用中慢慢體會(huì)它帶給我們開發(fā)的便利吧。你要的全在此塞蹭。
//1 RACSignal
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
NSLog(@"發(fā)送請(qǐng)求******");
[subscriber sendNext:@1];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
NSLog(@"RACDisposable");
}];
}];
[signal subscribeNext:^(id x) {
NSLog(@"每注冊(cè)一次就會(huì)發(fā)送一次請(qǐng)求 接收到數(shù)據(jù)1:%@",x);
}];
[signal subscribeNext:^(id x) {
NSLog(@"每注冊(cè)一次就會(huì)發(fā)送一次請(qǐng)求 接收到數(shù)據(jù)2:%@",x);
}];
//2 RACMulticastConnection 可防止多次執(zhí)行發(fā)送請(qǐng)求sendNext
RACMulticastConnection *connection = [signal publish];
[connection.signal subscribeNext:^(id x) {
NSLog(@"訂閱者1");
}];
[connection.signal subscribeNext:^(id x) {
NSLog(@"訂閱者2");
}];
[connection connect];
//3 RACSubject 是 RACSignal子類 sendNext會(huì)遍歷所有subscriber調(diào)用其sendNext
RACSubject *subject = [RACSubject subject];
[subject subscribeNext:^(id x) {
NSLog(@"RACSubject發(fā)送前第一個(gè)訂閱者*****%@",x);
}];
[subject subscribeNext:^(id x) {
NSLog(@"RACSubject發(fā)送前第二個(gè)訂閱者*****%@",x);
}];
[subject sendNext:@3];
//4 RACReplaySubject 是 RACSubject 子類
RACReplaySubject *replaySubject = [RACReplaySubject subject];
[replaySubject sendNext:@5];
[replaySubject subscribeNext:^(id x) {
NSLog(@"RACReplaySubject 1 發(fā)送后我才注冊(cè)***%@",x);
}];
[replaySubject subscribeNext:^(id x) {
NSLog(@"RACReplaySubject 2 發(fā)送后我才注冊(cè)***%@",x);
}];
//5 RACSequence處理了隊(duì)列或字典鍵值對(duì)(數(shù)組和字典中的元素 signal出的值:字典包裝了Tuple元組 數(shù)組是值本身)
NSArray *numbers = @[@2, @5, @8,@10];
[numbers.rac_sequence.signal subscribeNext:^(id x) {
NSLog(@"*****數(shù)組中的值是***%@",x);
}];
NSArray *mapNewArray = [[numbers.rac_sequence map:^id(id value) {
return [NSString stringWithFormat:@"Map reslut value**%@",value];
}] array];
NSLog(@"Map出的新Array****%@", mapNewArray);
NSDictionary *dictions = @{@"name":@"Bobie", @"age":@19, @"city":@"Robbin", @"number":@39};
[dictions.rac_sequence.signal subscribeNext:^(id x) {
RACTupleUnpack(NSString *nameValue, NSString *ageValue) = x;
NSLog(@"***字典中的鍵值對(duì)是****%@****%@",nameValue,ageValue);
}];
//6 RACCommand
_commond = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
//必須要返回信號(hào) 如無(wú)則返回空信號(hào)
// return [RACSignal empty];
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"發(fā)送請(qǐng)求"];
[subscriber sendCompleted];
return [RACDisposable disposableWithBlock:^{
}];
}];
}];
[_commond.executionSignals subscribeNext:^(id x) {
NSLog(@"***subscribeNext**out***%@",x);
[x subscribeNext:^(id x) {
NSLog(@"*****subscribeNext**in**%@",x);
}];
}];
[_commond.executionSignals.switchToLatest subscribeNext:^(id x) {
NSLog(@"**switchToLatest******%@",x);
}];
[[_commond.executing skip:1] subscribeNext:^(id x) {
if ([x boolValue] == YES) {
NSLog(@"正在執(zhí)行");
}else{
NSLog(@"執(zhí)行完成");
}
}];
[_commond execute:@6];
_firstBtn.rac_command = _commond;
//具體看下commond的示例:executionSignals是RACCommand的signal孽江,每當(dāng)command開始執(zhí)行時(shí)next:,其參數(shù)是由command創(chuàng)建的signal番电,所以這個(gè)executionSignals是一個(gè)值為signal的signal岗屏。在command每次開始執(zhí)行時(shí)得到一個(gè)包含字符串值的signal:
RACSignal *startSignal = [_commond.executionSignals map:^id(RACSignal *value) {
return NSLocalizedString(@"發(fā)送請(qǐng)求字符串", nil);
}];
//如果我們想用純粹的函數(shù)在command執(zhí)行完成后得到一個(gè)包含字符串的signal。flattenMap:方法在command執(zhí)行時(shí)钧舌,會(huì)調(diào)用block并傳入subscribeSignal担汤,這個(gè)block會(huì)返回一個(gè)新的signal,它的值就在這個(gè)要返回的signal中洼冻。materialize會(huì)將一個(gè)signal轉(zhuǎn)換為RACEvent信號(hào)(將一個(gè)signal的next:complete和error:消息轉(zhuǎn)換為RACEvent實(shí)例的next:的值)崭歧。接下來(lái)我們就過(guò)濾這些事件,只留下RACEventTypeCompleted完成事件撞牢,并將其map成一個(gè)字符串值率碾。
RACSignal *completeSignal = [_commond.executionSignals flattenMap:^RACStream *(RACSignal *value) {
return [[[value materialize] filter:^BOOL(RACEvent *value) {
return value.eventType == RACEventTypeCompleted;
}] map:^id(id value) {
return NSLocalizedString(@"Thanks", nil);
}];
}];
//效果等同于上面FlattenMap作用
@weakify(self);
[_commond.executionSignals subscribeNext:^(RACSignal*subscribeSignal) {
[subscribeSignal subscribeCompleted:^{
@strongify(self);
self.myString= @"Thanks";
}];
}];
//executionSignals屬性,這個(gè)signals不會(huì)發(fā)送了error事件屋彪,而是由errors這個(gè)屬性來(lái)發(fā)送的所宰。在一個(gè)command的執(zhí)行期間,如果一個(gè)signal發(fā)送了error畜挥,這會(huì)被signals當(dāng)成一個(gè)next:事件發(fā)送仔粥,而errors屬性則會(huì)發(fā)送這個(gè)錯(cuò)誤信息。errors屬性不會(huì)發(fā)送error:事件蟹但。我們很容易就能將錯(cuò)誤信息map成一個(gè)字符串消息
RACSignal *failSignal = [[_commond.errors subscribeOn:[RACScheduler mainThreadScheduler]] map:^id(NSError *error){
return NSLocalizedString(@"FailError", nil);
}];
//7 將這個(gè)3個(gè)signals合并成一個(gè)signal躯泰,并將其綁定到self的myString屬性上
RAC(self, myString) = [RACSignal merge:@[startSignal, completeSignal, failSignal]];
//8
@weakify(self);
//監(jiān)聽某個(gè)方法調(diào)用 表示只要self調(diào)用btnOnclickedFirst:,就會(huì)發(fā)出信號(hào),訂閱就好了华糖。
[[self rac_signalForSelector:@selector(btnOnclickedFirst:)] subscribeNext:^(id x) {
NSLog(@"**************3434343434");
}];
//監(jiān)聽某個(gè)屬性改變?yōu)樾轮? [[self rac_valuesAndChangesForKeyPath:@"mypoint" options:NSKeyValueObservingOptionNew observer:nil] subscribeNext:^(id x) {
NSLog(@"*****mypoint 新值為***%@",x);
}];
//監(jiān)聽某個(gè)屬性變化
[RACObserve(self, myString) subscribeNext:^(id x) {
NSLog(@"mystring 有變化 我知道了");
}];
//監(jiān)聽按鈕動(dòng)作
[[_firstBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"監(jiān)聽按鈕了");
}];
//監(jiān)聽輸入框輸入
[_firstTextField.rac_textSignal subscribeNext:^(id x) {
@strongify(self);
NSLog(@"文字改變了%@",x);
NSLog(@"********改變輸入框文字即改變myString**%@", self.myString);
}];
//處理多個(gè)請(qǐng)求麦向,都返回結(jié)果的時(shí)候,統(tǒng)一做處理.當(dāng)signalOne和signalTwo都至少sendNext過(guò)一次客叉,接下來(lái)只要其中任意一個(gè)signal有了新的內(nèi)容诵竭,updateMyUI:dateTwo:這個(gè)方法就會(huì)自動(dòng)被觸發(fā)
RACSignal *signalOne = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"第一個(gè)請(qǐng)求發(fā)送"];
return [RACDisposable disposableWithBlock:^{
}];
}];
RACSignal *signalTwo = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@"第二個(gè)請(qǐng)求發(fā)送"];
return [RACDisposable disposableWithBlock:^{
}];
}];
[self rac_liftSelector:@selector(updateMyUI:dateTwo:) withSignalsFromArray:@[signalOne, signalTwo]];
//給某個(gè)對(duì)象的某個(gè)屬性綁定
RAC(self, myString) = _firstTextField.rac_textSignal;
//監(jiān)聽通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(id x) {
NSLog(@"系統(tǒng)通知被監(jiān)聽");
}];