How can an NSOperationQueue wait for two async operations?

后端 未结 3 1437
情书的邮戳
情书的邮戳 2020-12-30 10:15

How can I make an NSOperationQueue (or anything else) wait for two async network calls with callbacks? The flow needs to look like this

Block Begins {
    Ne         


        
3条回答
  •  情书的邮戳
    2020-12-30 10:42

    Do you have to use NSOperation Queue? Here's how you can do it w/ a dispatch group:

    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
    [AsyncReq get:^{
        code
        dispatch_group_leave(group); 
    } onError:^(NSError *error) {
        code
        dispatch_group_leave(group);
    }];
    
    
    dispatch_group_enter(group);
    [AsyncReq get:^{
        code
        dispatch_group_leave(group); 
    } onError:^(NSError *error) {
        code
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"Both operations completed!")
    });
    

提交回复
热议问题