简体   繁体   中英

How to waiting for RACSequence finish it's task

example i have 2 task need to do:

task 1: save all product from array into DB. task 2: get products from DB.

Issue : task 2 running when task 1 is not finish yet. So i want task 1 finish and then task 2 will running after that

here is my code :

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

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

            return [self.storage saveProduct:product] ;

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

what i am wrong in this code above? Thanks for answer and sorry if my english is not good

I think what you need here is the concat operation. You can concat a stream of signals into one signal which will complete when all the concatenated signals send complete.

In your example you have several save operations which you want to wait for to finish first (concat their results) and then execute the loading operation.

I would phrase it like this with 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");
}];

Make sure to your concatenated save signals complete at some point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM