使用场景: 可以快速高效的遍历数组和字典。
创建一个model类:RACSequenceModel
创建数据模型:
NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
NSArray *dictArr = [NSArray arrayWithContentsOfFile:path];
//之所以在这打印主线程后面会用到。
NSLog(@"*****%@",[NSThread mainThread]);
[self.dataarr removeAllObjects];
for(NSDictionary*dicindictArr) {
RACSequenceModel*model = [RACSequenceModel new];
model.name= dic[@"name"];
model.icom= dic[@"icon"];
[self.dataarraddObject:model];
}
对数组进行遍历:
@weakify(self);
[self.dataarr.rac_sequence.signal subscribeNext:^(id _Nullable x) {
@strongify(self);
NSLog(@"%@",x);
//异步遍历
/**
2018-08-14 15:50:46.186494+0800 RACDemo[1890:45315] ***主线程**{number = 1, name = main}
2018-08-14 15:50:46.187972+0800 RACDemo[1890:45481]
2018-08-14 15:50:46.188438+0800 RACDemo[1890:45481] {number = 3, name = (null)}
2018-08-14 15:50:46.188847+0800 RACDemo[1890:45481]
2018-08-14 15:50:46.189458+0800 RACDemo[1890:45481] {number = 3, name = (null)}
*/
NSLog(@"%@",[NSThread currentThread]);
// 主线程执行:
dispatch_async(dispatch_get_main_queue(), ^{
// something
[self.contentL setText: [NSString stringWithFormat:@"%lu",(unsigned long)[self.dataarr indexOfObject:x]]];
});
}error:^(NSError*_Nullableerror) {
NSLog(@"===error===");
}completed:^{
// 主线程执行:
dispatch_async(dispatch_get_main_queue(), ^{
// something
self.contentL.text = @"遍历结束";
});
}];
网友评论