ios - 如何等待RACSequence完成它的任务

标签 ios reactive-cocoa

例如我有 2 个任务需要做:

任务 1:将数组中的所有产品保存到数据库中。 任务二:从数据库中获取产品。

问题:任务 2 在任务 1 尚未完成时运行。所以我希望任务 1 完成,然后任务 2 将在此之后运行

这是我的代码:

RACSignal *productsSignal = [[[[products rac_sequence] signalWithScheduler:

[RACScheduler currentScheduler]] flattenMap:^RACStream *(JMProduct *product) {

            return [self.storage saveProduct:product] ;

        }] then:^RACSignal *{
            return [self.storage getProductsFromLocalForModule:moduleId];
        }];

我在上面的这段代码中错了什么? 感谢您的回答,如果我的英语不好,抱歉

最佳答案

我想你在这里需要的是 concat手术。您可以将信号流连接成一个信号,当所有连接的信号发送完成时,该信号将完成。

在您的示例中,您有几个保存操作,您希望首先等待它们完成(连接它们的结果),然后执行加载操作。

我会用 RAC 2.x 来表达它

[[[RACSignal concat:

   // concat the results of save operations
   // concat will complete when all the save signals complete

   [@[@"product1", @"product2", @"product3"].rac_sequence
        map:^RACStream *(NSString* product) {
            NSLog(@"Save product: %@", product);

            // do sync / async save operations and which send complete when finished
            return [RACSignal return:@"Save product operation complete"];
        }]

] then:^RACSignal *{
    NSLog(@"Load products");

    // do sync / async load operation which sends complete when finished
    return [RACSignal return:@"Load product operation complete"];
}] subscribeNext:^(id x) {
    NSLog(@"Save and load finished");
}];

确保您的串联保存信号在某个时候完成。

关于ios - 如何等待RACSequence完成它的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32722233/

相关文章:

android - 防止 ios 应用程序出现 "fake-client"

ios - 当我设置过去的日期通知时,UILocal 通知不会按计划日期和时间触发

ios - 将可达性与 ReactiveCocoa 集成?

validation - ReactiveCocoa - 观察 isFirstResponder 属性和 UITextField 并将clearsOnBeginEditing 设置为 YES

ios - Reactivecocoa KVC 和实例对象之间的区别

ios - 尝试创建电子邮件时iOS应用程序崩溃

ios - 使用 iOS 每 x 分钟发送一次 GPS 坐标 - Core Location 不会停止更新

ios - cell.detailTextLabel.text 的问题(无重复)

swift - ReactiveCocoa 4 MVVM HTTP 请求最佳实践

ios - 如何使用 ReactiveCocoa 处理无效 token