美文网首页
RAC之RACSequence简单学习

RAC之RACSequence简单学习

作者: 带有bug的文艺青年 | 来源:发表于2018-08-14 16:13 被阅读39次

使用场景: 可以快速高效的遍历数组和字典。

创建一个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 = @"遍历结束";

        });

    }];

注意:遍历回调是异步线程回调,如果想要刷新UI需要回主线程进行刷新。

相关文章

网友评论

      本文标题:RAC之RACSequence简单学习

      本文链接:https://www.haomeiwen.com/subject/ocyvbftx.html