例如我有 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/