1、RACSignal
// 只要訂閱者調(diào)用sendNext,就會執(zhí)行nextBlock
? ?// 只要訂閱RACDynamicSignal,就會執(zhí)行didSubscribe
? ?// 前提條件是RACDynamicSignal,不同類型信號的訂閱,處理訂閱的事情不一樣
//創(chuàng)建信號均驶,此時信號是冷信號多柑,并不能發(fā)送數(shù)據(jù)RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? [subscriber sendNext:@"發(fā)送數(shù)據(jù)"];
//信號銷毀
return[RACDisposable disposableWithBlock:^{
? ? ? ? ? ? NSLog(@"信號取消訂閱");
? ? ? ? }];
? ? }];
? ? //信號訂閱,此時信號變?yōu)闊嵝盘柧ソ兀邮盏綌?shù)據(jù)[signal subscribeNext:^(id x) {
?// nextBlock調(diào)用:只要訂閱者發(fā)送數(shù)據(jù)就會調(diào)用
? ? ? ? NSLog(@"%@", x);
? ? }];
2、RACSubject
//創(chuàng)建信號RACSubject *subject = [RACSubject subject];
? ? /*? ? ? 不同信號訂閱的方式不一樣
? ? ? RACSubject處理訂閱:僅僅是保存訂閱者,可以多個訂閱者
? ? ? 底層實現(xiàn):遍歷所有的訂閱者,調(diào)用nextBlock
? ? ? 執(zhí)行流程:
? ? ? RACSubject被訂閱,僅僅是保存訂閱者
? ? ? RACSubject發(fā)送數(shù)據(jù),遍歷所有的訂閱,調(diào)用他們的nextBlock
? ? */? ? [subject subscribeNext:^(id x) {
? ? ? ? NSLog(@"++++++%@", x);
? ? }];
? ? [subject subscribeNext:^(id x) {
? ? ? ? NSLog(@"-----%@", x);
? ? }];
? ? [subject sendNext:@111];
?3、RACReplaySubject
//創(chuàng)建信號RACReplaySubject *replaySubject = [RACReplaySubject subject];//可以先發(fā)送數(shù)據(jù)再訂閱信號[replaySubject sendNext:@111111];
[replaySubject subscribeNext:^(id x) {
? ? ? ? NSLog(@"---%@", x);
? ? }];
? ? [replaySubject subscribeNext:^(id x) {
? ? ? ? NSLog(@"+++++%@", x);
? ? }];
4案训、RAC中的KVO、通知粪糙、監(jiān)聽事件强霎、監(jiān)聽文本框、計時器
//KVO
//方式一:其中_redView是屬性UIView蓉冈,監(jiān)聽frame變化
[_redView rac_observeKeyPath:@"frame"options:NSKeyValueObservingOptionNew observer:nil block:^(idvalue, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {//value就是frame變化之后的值NSLog(@"----%@", value);? ? }];
//方式二:[[_redView rac_valuesForKeyPath:
@"frame"observer:nil] subscribeNext:^(id x) {
? ? ? ? NSLog(@"+++++%@", x);
? ? }];//監(jiān)聽事件城舞,監(jiān)聽按鈕的點擊事件[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
? ? ? ? NSLog(@"控制器按鈕被點擊");? ? }];
//_label增加點擊手勢,監(jiān)聽點擊事件UITapGestureRecognizer
*tap = [[UITapGestureRecognizer alloc]init];
? ? [[tap rac_gestureSignal] subscribeNext:^(UITapGestureRecognizer *x) {
? ? ? ? NSLog(@"----label增加單擊手勢%@", x);
? ? }];
? ? [_label addGestureRecognizer:tap];
? ? //通知寞酿,監(jiān)聽鍵盤彈起的通知[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardWillShowNotificationobject:nil] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@", x);
? ? }];
? ? //監(jiān)聽文本框家夺,只要文本框中內(nèi)容一產(chǎn)生變化,就會走這個block[_textField.rac_textSignal subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@", x);
? ? }];
? ? [[_textField rac_signalForControlEvents:UIControlEventEditingDidEndOnExit] subscribeNext:^(id x) {
? ? ? ? //return之后隱藏鍵盤
? ? ? ? //x是TextFieldNSLog(@"%@",x);
? ? }];
? ? //延遲一定時間做某事[[RACScheduler mainThreadScheduler]afterDelay:2.0fschedule:^{
? ? ? ? NSLog(@"2秒之后發(fā)生的事情");
? ? }];
? ? // 每個一定長度時間做一件事伐弹,類似NSTimer[[RACSignal interval:1onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * date) {
? ? ? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
? ? ? ? [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
? ? ? ? _label.text = [formatter stringFromDate:date];
? ? ? ? //? ? ? ? NSLog(@"%@",[formatter stringFromDate:date]);}];
//監(jiān)聽UIAlertView彈框的點擊事件UIAlertView
* alertView = [[UIAlertView alloc]initWithTitle:@"警告"message:@"是否確認登錄?"delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"確定", nil];
//實現(xiàn)alertView的點擊事件的delegate方法[[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:
@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tuple) {? ? ? ? //點擊取消拉馋、確定按鈕會到本blockRACTupleUnpack(UIAlertView*alert, NSNumber *index) = tuple;
? ? ? ? NSLog(@"alertView : %@------%@",alert, index);? ? }];? ? [alertView show]; //類似UIAlertViewUIActionSheet*sheet = [[UIActionSheet alloc]initWithTitle:nildelegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相機", @"相冊", nil];
? ? [[self rac_signalForSelector:@selector(actionSheet:clickedButtonAtIndex:) fromProtocol:@protocol(UIActionSheetDelegate)] subscribeNext:^(RACTuple * tuple) {
? ? ? ? RACTupleUnpack(UIActionSheet *alert, NSNumber *index) = tuple;
? ? ? ? NSLog(@"alertView : %@------%@",alert, index);
? ? }];
? ? [sheet showInView:self.view];
5、RAC中的集合:
//類似NSArray
RACTuple *tuple = [RACTuple tupleWithObjectsFromArray:@[@1,@"dssds", @3]];
? ? NSNumber *first = tuple[0];
? ? NSLog(@"%@", first);
? ? //arrayNSArray *array = @[@"123",@"456",@"789"];
? ? //RAC集合RACSequence *sequence = array.rac_sequence;
? ? //轉(zhuǎn)化為信號RACSignal *signal = sequence.signal;
? ? [signal subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@", x);? ? }];
//聯(lián)合起來遍歷數(shù)據(jù)就是:[array.rac_sequence.signal subscribeNext:
^(id x) {
? ? ? ? NSLog(@"------%@", x);
? ? }];
? ? //DictNSDictionary *dict = @{@"name":@"小明",@"age":@"20"};
? ? [dict.rac_sequence.signal subscribeNext:^(RACTuple *x) {
? ? ? ? //? ? ? ? NSString *key = x[0];
? ? ? ? //? ? ? ? NSString *value = x[1];
? ? ? ? //? ? ? ? NSLog(@"key : %@, value : %@", key, value);
? ? ? ? // RACTupleUnpack:用來解析元組
? ? ? ? // 宏里面的參數(shù),傳需要解析出來的變量名
? ? ? ? // = 右邊,放需要解析的元組RACTupleUnpack(NSString *key, NSString *value) = x;
? ? ? ? NSLog(@"%@--%@", key, value);
? ? }];
? ? NSArray *allModels = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil]];
? ? //? ? NSMutableArray *models = [NSMutableArray array];
? ? //? ? [allModels.rac_sequence.signal subscribeNext:^(NSDictionary *x) {
? ? ////? ? ? ? FlagModel *model = [FlagModel flagWithDict:x];
? ? //? ? ? ? [models addObject:model];
? ? ////}];
? ? //models數(shù)組中的數(shù)據(jù)是已經(jīng)將數(shù)據(jù)轉(zhuǎn)好的模型數(shù)據(jù)NSArray *models = [[allModels.rac_sequence map:^id(idvalue) {
//字典轉(zhuǎn)模型
return [FlagModel flagWithDict:value];
? ? }] array];
? ? NSLog(@"-----%@", models);
6惨好、RACMulticastConnection
//弊端:有幾個訂閱者就會請求幾次數(shù)據(jù)
? ? // 1.創(chuàng)建信號RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? NSLog(@"發(fā)送熱門模塊的請求");
? ? ? ? // 3.發(fā)送數(shù)據(jù)[subscriber sendNext:@1];
? ? ? ? return nil;
? ? }];
? ? // 2.訂閱信號[signal subscribeNext:^(id x) {
? ? ? ? NSLog(@"訂閱者一%@",x);
? ? }];
? ? [signal subscribeNext:^(id x) {
? ? ? ? NSLog(@"訂閱者二%@",x);
? ? }];
? ? //弊端優(yōu)化
? ? // 不管訂閱多少次信號,就會請求一次
? ? // RACMulticastConnection:必須要有信號
? ? // 1.創(chuàng)建信號RACSignal *signal2 = [RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? // didSubscribe什么時候來:連接類連接的時候NSLog(@"發(fā)送熱門模塊的請求");
? ? ? ? [subscriber sendNext:@"熱門模塊的數(shù)據(jù)"];
? ? ? ? return nil;
? ? }];
? ? // 2.把信號轉(zhuǎn)換成連接類
? ? // 確定源信號的訂閱者RACSubject
? ? //? ? RACMulticastConnection *connection = [signal publish];RACMulticastConnection *connection = [signal2 multicast:[RACReplaySubject subject]];
? ? // 3.訂閱連接類信號[connection.signal subscribeNext:^(id x) {
? ? ? ? // nextBlock:發(fā)送數(shù)據(jù)就會來NSLog(@"訂閱者1:%@",x);
? ? }];
? ? // 4.連接? ? [connection connect];
7煌茴、RACCommand
//創(chuàng)建RACCommand,RACCommand命令在RAC很常用日川,常用語網(wǎng)絡請求
RACCommand *command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
? ? ? ? NSLog(@"---input = %@", input);
? ? ? ? //11return[RACSignal createSignal:^RACDisposable *(idsubscriber) {
? ? ? ? ? ? //網(wǎng)絡請求蔓腐,將請求的數(shù)據(jù)發(fā)送出去[subscriber sendNext:
@"網(wǎng)絡請求的數(shù)據(jù)"];
? ? ? ? ? ? return nil;
? ? ? ? }];
? ? }];
? //取到command創(chuàng)建的RACSignal
? ? //方式一:RACSignal *signal = [command execute:@1];
? ? [signal subscribeNext:^(id x) {
? ? ? NSLog(@"---%@", x);
? }];
? ? //方式2:
? ? // 注意:必須要在執(zhí)行命令前,訂閱
? ? // executionSignals:信號源,信號中信號,signalOfSignals:信號:發(fā)送數(shù)據(jù)就是信號[command.executionSignals subscribeNext:^(RACSignal *x) {
? ? ? NSLog(@"%@", x);
? ? ? [x subscribeNext:^(id x) {
? ? ? ? ? NSLog(@"%@", x);
? ? ? }];
? }];
? //方式3: switchToLatest獲取最新發(fā)送的信號,只能用于信號中信號[command.executionSignals.switchToLatest subscribeNext:^(id x) {
? ? ? ? NSLog(@"--x = %@", x);
? ? }];
? [command execute:@23];
? ? ? ? //下面對信號中的信號講解:
? ? // 創(chuàng)建信號中信號RACSubject *signalOfSignals = [RACSubject subject];
? ? RACSubject *signalA = [RACSubject subject];
? ? RACSubject *signalB = [RACSubject subject];
? ? // 訂閱信號
? ? //? ? [signalOfSignals subscribeNext:^(RACSignal *x) {
? ? //? ? ? ? [x subscribeNext:^(id x) {
? ? //? ? ? ? ? ? NSLog(@"%@",x);
? ? //? ? ? ? }];
? ? //? ? }];
? ? // switchToLatest:獲取信號中信號發(fā)送的最新信號[signalOfSignals.switchToLatest subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? // 發(fā)送信號? ? [signalOfSignals sendNext:signalA];
? ? [signalA sendNext:@1];
? ? [signalB sendNext:@"BB"];
? ? [signalA sendNext:@"11"];
? //**********************************************************************************************************************
? ? // 當前命令內(nèi)部發(fā)送數(shù)據(jù)完成,一定要主動發(fā)送完成
? ? // 1.創(chuàng)建命令RACCommand *command2 = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
? ? ? ? // input:執(zhí)行命令傳入?yún)?shù)
? ? ? ? // Block調(diào)用:執(zhí)行命令的時候就會調(diào)用NSLog(@"%@",input);
? ? ? ? return[RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? ? ? // 發(fā)送數(shù)據(jù)[subscriber sendNext:@"執(zhí)行命令產(chǎn)生的數(shù)據(jù)"];
? ? ? ? ? ? // 發(fā)送完成? ? ? ? ? ? [subscriber sendCompleted];
? ? ? ? ? ? return nil;
? ? ? ? }];
? ? }];
? ? // 監(jiān)聽事件有沒有完成[command2.executing subscribeNext:^(id x) {
? ? ? ? if([x boolValue] == YES) {// 當前正在執(zhí)行NSLog(@"當前正在執(zhí)行");
? ? ? ? }else{
? ? ? ? ? ? // 執(zhí)行完成/沒有執(zhí)行NSLog(@"執(zhí)行完成/沒有執(zhí)行");
? ? ? ? }
? ? }];
? ? // 2.執(zhí)行命令[command2 execute:@1];
8、bind逗鸣,信號的綁定
// 1.創(chuàng)建信號RACSubject *subject = [RACSubject subject];
? ? // 2.綁定信號RACSignal *bindSignal = [subject bind:^RACStreamBindBlock{
? ? ? ? // block調(diào)用時刻:只要綁定信號被訂閱就會調(diào)用return^RACSignal *(idvalue, BOOL *stop){
? ? ? ? ? ? // block調(diào)用:只要源信號發(fā)送數(shù)據(jù),就會調(diào)用block
? ? ? ? ? ? // block作用:處理源信號內(nèi)容
? ? ? ? ? ? // value:源信號發(fā)送的內(nèi)容? ? ? ? ? ?
? ? ? ? ? ? NSLog(@"接收到原信號的內(nèi)容:%@",value);
? ? ? ? ? ? value = [NSString stringWithFormat:@"xmg:%@",value];
? ? ? ? ? ? // 返回信號,不能傳nil,返回空信號[RACSignal empty]return[RACReturnSignalreturn:value];
? ? ? ? };
? ? }];
? ? // 3.訂閱綁定信號[bindSignal subscribeNext:^(id x) {
? ? ? ? // blcok:當處理完信號發(fā)送數(shù)據(jù)的時候,就會調(diào)用這個BlockNSLog(@"接收到綁定信號處理完的信號%@",x);
? ? }];
? ? // 4.發(fā)送數(shù)據(jù)[subject sendNext:@"123"];
9合住、map绰精、flattenMap
//map、flattenMap主要是對發(fā)送的數(shù)據(jù)進行編輯透葛,如:可以將網(wǎng)絡請求的數(shù)據(jù)轉(zhuǎn)化的模型在發(fā)送
// 創(chuàng)建信號,RACSubject *subject2 = [RACSubject subject];
? ? // 綁定信號RACSignal *bindSignal2 = [subject2 map:^id(id value) {
? ? ? ? // 返回的類型,就是你需要映射的值return[NSString stringWithFormat:@"請求的數(shù)據(jù):%@",value];
? ? }];
? ? // 訂閱綁定信號[bindSignal2 subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? [subject2 sendNext:@"123"];//******************flattenMap一般用于信號中的信號*******************************RACSubject *signalOfsignals = [RACSubject subject];
? ? RACSubject *signal = [RACSubject subject];
[[signalOfsignals flattenMap:^RACStream *(id value) {
? ? ? ? return value;
? ? }] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? // 發(fā)送信號? ? [signalOfsignals sendNext:signal];
? ? [signal sendNext:@"213"];
10笨使、組合
// 組合,兩個信號都完成才執(zhí)行
? ? // concat:皇上,皇太子
? ? // 創(chuàng)建信號ARACSignal *siganlA = [RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? // 發(fā)送請求NSLog(@"發(fā)送上部分請求");
? ? ? ? // 發(fā)送信號[subscriber sendNext:@"上部分數(shù)據(jù)"];
? ? ? ? [subscriber sendCompleted];
? ? ? ? return nil;
? ? }];
? ? RACSignal *siganlB = [RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? // 發(fā)送請求NSLog(@"發(fā)送下部分請求");
? ? ? ? // 發(fā)送信號[subscriber sendNext:@"下部分數(shù)據(jù)"];
? ? ? ? return nil;
? ? }];
? ? // concat:按順序去連接
? ? // 注意:concat,第一個信號必須要調(diào)用sendCompleted
? ? // 創(chuàng)建組合信號RACSignal *concatSignal = [siganlA concat:siganlB];
? ? // 訂閱組合信號[concatSignal subscribeNext:^(id x) {
? ? ? ? // 既能拿到A信號的值,又能拿到B信號的值NSLog(@"%@",x);
? ? }];
? ? //***************************************************************************************************
? ? // 創(chuàng)建信號ARACSignal *siganl1 = [RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? // 發(fā)送請求NSLog(@"發(fā)送上部分請求");
? ? ? ? // 發(fā)送信號[subscriber sendNext:@"上部分數(shù)據(jù)"];
? ? ? ? // 發(fā)送完成? ? ? ? [subscriber sendCompleted];
? ? ? ? return nil;
? ? }];
? ? // 創(chuàng)建信號BRACSignal *siganl2 = [RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? // 發(fā)送請求NSLog(@"發(fā)送下部分請求");
? ? ? ? // 發(fā)送信號[subscriber sendNext:@"下部分數(shù)據(jù)"];
? ? ? ? return nil;
? ? }];
? ? // 創(chuàng)建組合信號
? ? // then:忽悠掉第一個信號所有值RACSignal *thenSiganl = [siganl1 then:^RACSignal *{
? ? ? ? // 返回信號就是需要組合的信號return siganl2;
? ? }];
? ? // 訂閱信號[thenSiganl subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? //***************************************************************************************************
? ? // 創(chuàng)建信號ARACSubject *signalA = [RACSubject subject];
? ? // 創(chuàng)建信號BRACSubject *signalB = [RACSubject subject];
? ? // 組合信號RACSignal *mergeSiganl = [signalA merge:signalB];
? ? // 訂閱信號[mergeSiganl subscribeNext:^(id x) {
? ? ? ? // 任意一個信號發(fā)送內(nèi)容都會來這個blockNSLog(@"%@",x);
? ? }];
? ? // 發(fā)送數(shù)據(jù)[signalB sendNext:@"下部分"];
? ? [signalA sendNext:@"上部分"];
? ? //***************************************************************************************************
? ? // zipWith:夫妻關系
? ? // 創(chuàng)建信號ARACSubject *signalC = [RACSubject subject];
? ? // 創(chuàng)建信號BRACSubject *signalD = [RACSubject subject];
? ? // 壓縮成一個信號
? ? // zipWith:當一個界面多個請求的時候,要等所有請求完成才能更新UI
? ? // zipWith:等所有信號都發(fā)送內(nèi)容的時候才會調(diào)用RACSignal *zipSignal = [signalC zipWith:signalD];
? ? // 訂閱信號[zipSignal subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? // 發(fā)送信號[signalD sendNext:@2];
? ? [signalC sendNext:@1];
? ? //***************************************************************************************************
? ? // 組合
? ? // 組合哪些信號
? ? // reduce:聚合
? ? // reduceBlock參數(shù):根組合的信號有關,一一對應RACSignal *comineSiganl = [RACSignal combineLatest:@[_accountFiled.rac_textSignal,_pwdField.rac_textSignal] reduce:^id(NSString *account,NSString *pwd){
? ? ? ? // block:只要源信號發(fā)送內(nèi)容就會調(diào)用,組合成新一個值NSLog(@"%@ %@",account,pwd);
? ? ? ? // 聚合的值就是組合信號的內(nèi)容return@(account.length && pwd.length);
? ? }];
? ? // 訂閱組合信號
? ? //? ? [comineSiganl subscribeNext:^(id x) {
? ? //? ? ? ? _loginBtn.enabled = [x boolValue];
? ? //? ? }];? ?
? ? RAC(_loginBtn,enabled) = comineSiganl;
11、過濾
// 只有當我們文本框的內(nèi)容長度大于5,才想要獲取文本框的內(nèi)容[[_textField.rac_textSignal filter:^BOOL(id value) {
? ? ? ? // value:源信號的內(nèi)容return[value length] >5;
? ? ? ? // 返回值,就是過濾條件,只有滿足這個條件,才能能獲取到內(nèi)容? ? ? ?
? ? }] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? //***************************************************************************************************
? ? // ignore:忽略一些值
? ? // ignoreValues:忽略所有的值
? ? // 1.創(chuàng)建信號RACSubject *subject = [RACSubject subject];
? ? // 2.忽略一些RACSignal *ignoreSignal = [subject ignoreValues];
? ? // 3.訂閱信號[ignoreSignal subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? // 4.發(fā)送數(shù)據(jù)[subject sendNext:@"13"];
? ? [subject sendNext:@"2"];
? ? [subject sendNext:@"44"];
? ? //***************************************************************************************************
? ? // 1.創(chuàng)建信號RACSubject *subject2 = [RACSubject subject];
? ? RACSubject *signal = [RACSubject subject];
? ? // take:取前面幾個值
? ? // takeLast:取后面多少個值.必須要發(fā)送完成
? ? // takeUntil:只要傳入信號發(fā)送完成或者發(fā)送任意數(shù)據(jù),就不能在接收源信號的內(nèi)容[[subject2 takeUntil:signal] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? [subject2 sendNext:@"1"];
? ? //? ? [signal sendNext:@1];
? ? //? ? [signal sendCompleted];? ? [signal sendError:nil];
? ? [subject2 sendNext:@"2"];
? ? [subject2 sendNext:@"3"];
? ? //***************************************************************************************************
? ? // distinctUntilChanged:如果當前的值跟上一個值相同,就不會被訂閱到
? ? // 1.創(chuàng)建信號RACSubject *subject3 = [RACSubject subject];
? ? [[subject3 distinctUntilChanged] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? [subject3 sendNext:@"1"];
? ? [subject3 sendNext:@"2"];
? ? [subject3 sendNext:@"2"];
? ? //***************************************************************************************************
? ? // skip;跳躍幾個值
? ? // 1.創(chuàng)建信號RACSubject *subject4 = [RACSubject subject];
? ? [[subject4 skip:2] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? [subject4 sendNext:@"1"];
? ? [subject4 sendNext:@"2"];
? ? [subject4 sendNext:@"3"];
12僚害、RAC其他一些操作
//******************************************** [[[[self.btn rac_signalForControlEvents:UIControlEventTouchUpInside]
? ? ? doNext:^(id x) {
? ? ? ? ? [self.btn setTitle:@"你好" forState:UIControlStateNormal];
? ? ? }]
? ? ? map:^id(id value) {
? ? ? ? ? return @(YES);
? ? ? }]
? ? subscribeNext:^(NSNumber *x) {
? ? ? ? self.btn.backgroundColor = x ? [UIColor orangeColor] : [UIColor cyanColor];
? ? }];
? ? //**************************************************************
? ? //doNext: 執(zhí)行Next之前硫椰,會先執(zhí)行這個Block
? ? //doCompleted: 執(zhí)行sendCompleted之前,會先執(zhí)行這個Block? ?
? ? [[[[RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? [subscriber sendNext:@1];
? ? ? ? [subscriber sendCompleted];
? ? ? ? return nil;
? ? }] doNext:^(id x) {
? ? ? ? // 執(zhí)行[subscriber sendNext:@1];之前會調(diào)用這個BlockNSLog(@"doNext");;
? ? }] doCompleted:^{
? ? ? ? // 執(zhí)行[subscriber sendCompleted];之前會調(diào)用這個BlockNSLog(@"doCompleted");;
? ? }] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? //*********************************************************
? ? //timeout:超時萨蚕,可以讓一個信號在一定的時間后靶草,自動報錯。? ?
? ? RACSignal *signal = [[RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? return nil;
? ? }] timeout:1 onScheduler:[RACScheduler currentScheduler]];
? ? [signal subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? } error:^(NSError *error) {
? ? ? ? // 1秒后會自動調(diào)用NSLog(@"%@",error);
? ? }];
? ? //interval 定時:每隔一段時間發(fā)出信號[[RACSignal interval:1onScheduler:[RACScheduler currentScheduler]] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? //delay 延遲發(fā)送next岳遥。[[[RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? [subscriber sendNext:@1];
? ? ? ? return nil;
? ? }] delay:2] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
? ? //*********************************************************
? ? //retry重試 :只要失敗奕翔,就會重新執(zhí)行創(chuàng)建信號中的block,直到成功.__blockinti =0;
? ? [[[RACSignal createSignal:^RACDisposable *(id subscriber) {
? ? ? ? if(i ==10) {
? ? ? ? ? ? [subscriber sendNext:@1];
? ? ? ? }else{
? ? ? ? ? ? NSLog(@"接收到錯誤");
? ? ? ? ? ? [subscriber sendError:nil];
? ? ? ? }
? ? ? ? i++;
? ? ? ? return nil;
? ? }] retry] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? } error:^(NSError *error) {
? ? }];
? ? //*********************************************************
? ? //replay重放:當一個信號被多次訂閱,反復播放內(nèi)容RACSignal *signal3 = [[RACSignal createSignal:^RACDisposable* (id subscriber) {
? ? ? ? [subscriber sendNext:@1];
? ? ? ? [subscriber sendNext:@2];
? ? ? ? return nil;
? ? }] replay];
? ? [signal3 subscribeNext:^(id x) {
? ? ? ? NSLog(@"第一個訂閱者%@",x);
? ? }];
? ? [signal3 subscribeNext:^(id x) {
? ? ? ? NSLog(@"第二個訂閱者%@",x);
? ? }];
? ? //*********************************************************
? ? //throttle節(jié)流:當某個信號發(fā)送比較頻繁時,可以使用節(jié)流浩蓉,在某一段時間不發(fā)送信號內(nèi)容派继,過了一段時間獲取信號的最新內(nèi)容發(fā)出。RACSubject *signal4 = [RACSubject subject];
? ? //? ? _signal = signal;
? ? // 節(jié)流捻艳,在一定時間(1秒)內(nèi)驾窟,不接收任何信號內(nèi)容,過了這個時間(1秒)獲取最后發(fā)送的信號內(nèi)容發(fā)出认轨。[[signal4 throttle:1] subscribeNext:^(id x) {
? ? ? ? NSLog(@"%@",x);
? ? }];
13绅络、RAC中常用的宏:
//1、防止循環(huán)引用嘁字,類似__weak typeof(self)waekSelf = self; @weakify(self)
[_textField.rac_textSignal subscribeNext:^(NSString *x) {
? ? @strongify(self)
self.textField.text = x;
}];//2恩急、包裝元組RACTuple *tuple = RACTuplePack(@1, @2);
? ? NSLog(@"---%@", tuple[0]);
? ? //之前賦值方式//? ? [_textField.rac_textSignal subscribeNext:^(id x) {////? ? ? ? _label.text = x;////? ? }];
? ? // 用來給某個對象的某個屬性綁定信號,只要產(chǎn)生信號內(nèi)容,就會把內(nèi)容給屬性賦值RAC(_label, text) = _textField.rac_textSignal;