当前位置:首页 > 编程语言 > 正文

rac 数组之遍历

11-18 编程语言

标签:str   thread   nsa   reads   nbsp   read   sch   ble   bsp   

rac的数组遍历其实很简单。但是有个点需要注意。

以下先举个例子说明遍历的用法

 NSArray *temArr = @[@"111",@"222"];
    [temArr.rac_sequence.signal subscribeNext:^(RACTuple * _Nullable x) {
        NSLog(@"%@",[NSThread currentThread]);
        NSLog(@"---数组遍历----%@",x);
    }];

以下是日志:

<NSThread: 0x281cb4340>{number = 4, name = (null)}

2019-11-16 11:29:26.322717 0800 NewProjectToTest[4289:1922998] ---数组遍历----111

2019-11-16 11:29:26.323227 0800 NewProjectToTest[4289:1922998] <NSThread: 0x281cb4340>{number = 4, name = (null)}

2019-11-16 11:29:26.323333 0800 NewProjectToTest[4289:1922998] ---数组遍历----222

 

很明显能看到代码不是在主线程中执行的。这样会导致我们在实际使用过程中需要考虑代码的执行问题。

所以我们在遍历的时候可以通过deliverOn把这个任务添加到主线程中,代码如下

[[temArr.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id  _Nullable x) {
        NSLog(@"=====%@",[NSThread currentThread]);
        NSLog(@"--3333-数组遍历----%@",x);
    }];

显示日志如下:

=====<NSThread: 0x281cde040>{number = 1, name = main}

2019-11-16 11:29:26.331387 0800 NewProjectToTest[4289:1922792] --3333-数组遍历----111

2019-11-16 11:29:26.331451 0800 NewProjectToTest[4289:1922792] =====<NSThread: 0x281cde040>{number = 1, name = main}

2019-11-16 11:29:26.331489 0800 NewProjectToTest[4289:1922792] --3333-数组遍历----222

 

差不多就这样吧。。很简单的使用。。

 

rac 数组之遍历

标签:str   thread   nsa   reads   nbsp   read   sch   ble   bsp   

温馨提示: 本文由杰米博客推荐,转载请保留链接: https://www.jmwww.net/file/biancheng/12079.html