rac的數組遍歷其實很簡單。可是有個點須要注意。數組
如下先舉個例子說明遍歷的用法spa
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] ---數組遍歷----111code
2019-11-16 11:29:26.323227+0800 NewProjectToTest[4289:1922998] <NSThread: 0x281cb4340>{number = 4, name = (null)}blog
2019-11-16 11:29:26.323333+0800 NewProjectToTest[4289:1922998] ---數組遍歷----222class
很明顯能看到代碼不是在主線程中執行的。這樣會致使咱們在實際使用過程當中須要考慮代碼的執行問題。遍歷
因此咱們在遍歷的時候能夠經過deliverOn把這個任務添加到主線程中,代碼以下di
[[temArr.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id _Nullable x) { NSLog(@"=====%@",[NSThread currentThread]); NSLog(@"--3333-數組遍歷----%@",x); }];
顯示日誌以下:co
=====<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
差很少就這樣吧。。很簡單的使用。。