Does dispatch_async(dispatch_get_main_queue(), ^{…}); wait until done?

后端 未结 8 1687
一整个雨季
一整个雨季 2020-11-28 20:45

I have a scenario in my app, where I want to do some time consuming task which consists of some data processing as well as UI update, in a method. My method looks like this,

8条回答
  •  清酒与你
    2020-11-28 20:53

    The good practice is: Dispatch Groups

    dispatch_group_t imageGroup = dispatch_group_create();
    
    dispatch_group_enter(imageGroup);
    [uploadImage executeWithCompletion:^(NSURL *result, NSError* error){
        // Image successfully uploaded to S3
        dispatch_group_leave(imageGroup);
    }];
    
    dispatch_group_enter(imageGroup);
    [setImage executeWithCompletion:^(NSURL *result, NSError* error){
        // Image url updated
        dispatch_group_leave(imageGroup);
    }];
    
    dispatch_group_notify(imageGroup,dispatch_get_main_queue(),^{
        // We get here when both tasks are completed
    });
    

提交回复
热议问题