RACCommand中的信號


示例:

 RACSignal* textSignal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@(1)];
        [subscriber sendNext:@(2)];
        [subscriber sendError:[NSError new]];
        return nil;
    }];
    
    RACCommand* textCommad = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        return textSignal;
    }];
    
    self.createButton.rac_command = textCommad;
    
    [textCommad.executing subscribeNext:^(id x) {
        NSLog(@"executing%@",x);
    }];
    
    [textCommad.executionSignals subscribeNext:^(id x) {
        NSLog(@"executionSignals%@",x);
    }];
    
    [[textCommad.executionSignals switchToLatest]subscribeNext:^(id x) {
        NSLog(@"executionSignals switchLatest%@",x);
    }];
    
    [textCommad.errors subscribeNext:^(id x) {
        NSLog(@"errors");
    }];

輸出:

2015-08-09 22:17:27.610 ReactiveCocoaDemo[7181:141024] executing0

2015-08-09 22:17:30.325 ReactiveCocoaDemo[7181:141024] executing1

2015-08-09 22:17:30.325 ReactiveCocoaDemo[7181:141024] executionSignals<RACDynamicSignal: 0x7f89b3c6ca20> name: 

2015-08-09 22:17:30.326 ReactiveCocoaDemo[7181:141024] executionSignals switchLatest1

2015-08-09 22:17:30.326 ReactiveCocoaDemo[7181:141024] executionSignals switchLatest2

2015-08-09 22:17:30.327 ReactiveCocoaDemo[7181:141024] errors

2015-08-09 22:17:30.327 ReactiveCocoaDemo[7181:141024] executing0

結論:

1.executing信號一綁定就會sendNext:@(NO);如果想忽略第一次的Next,使用[executing skip:1]

2.按鈕點擊事件發生時,首先exectuing會sendNext:@(YES);然后executionSignals會sendNext一個RACSignal對象,該對象就是RACCommand創建時傳入的block的返回值,。 改正:該RACSignal對象與RACCommand創建時傳入的block的返回值不是同一個對象,但是他們兩個會sendNext相同的值。

3.注意executionSignals是信號的信號,即它的值類型為RACSignal,而我們一般希望捕獲的是RACSignal所攜帶的值,因此可以使用switchToLatest或flatten的方法來做到;也可以直接對該RACSignal進行訂閱。

4.errors包含了RACComand執行過程產生的所有錯誤。

5.等到RACCommand中的Signal都完畢了(complete或error),exectuting會sendNext:@(NO).


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM