Need to make two HTTP network requests simultaneously (with a completion handler once both finish)

后端 未结 2 1762
轻奢々
轻奢々 2020-12-28 10:22

I have a situation where I need to make two HTTP GET requests and handle their results only after both are finished. I have a completion handler on each individual network r

2条回答
  •  不知归路
    2020-12-28 10:52

    Source: How do I write dispatch_after GCD in Swift 3? You can use dispatch_group for that. For example (ObjC code):

    dispatch_group_t group = dispatch_group_create();
    
    //startOperation1
    dispatch_group_enter(group);
    
    //finishOpeartion1
    dispatch_group_leave(group);
    
    
    //startOperation2
    dispatch_group_enter(group);
    
    //finishOpeartion2
    dispatch_group_leave(group);
    
    
    //Handle both operations completion
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 
    //code here
    });
    

提交回复
热议问题