在我的应用程序中,我具有照片上传功能,并且希望我的主队列等待照片上传完成。这是我的代码:

    dispatch_group_t groupe = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("com.freesale.chlebta.photoUplaod", 0);

dispatch_group_async(groupe, queue, ^{

    //Upload photo in same array with annonce
    //++++++++++++++++++++++++++++++++++++++
    if(!_annonce)
        [KVNProgress updateStatus:@"جاري رفع الصور"];

    __block NSInteger numberPhotoToUpload = _photoArray.count - 1;

    for (int i = 1; i < _photoArray.count; i++) {
        //check if image is asset then upload it else just decrement the photo numver because it's already uploaded
        if ( [[_photoArray objectAtIndex:i] isKindOfClass:[ALAsset class]]){
            ALAsset *asset = [_photoArray objectAtIndex:i];

            NSData *imageData = UIImageJPEGRepresentation([UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]], 0.6);

            PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
            [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded)
                    [annonce addUniqueObject:imageFile forKey:@"photo"];
                else
                    NSLog(@"Error image upload \n image :%i \n error:  %@",i, error);

                    numberPhotoToUpload --;
            }];
        } else
            numberPhotoToUpload --;

    }

});

//Wait until Photo Upload Finished

dispatch_group_wait(groupe, DISPATCH_TIME_FOREVER);

// Some other Operation


但这没有用,我的程序可以继续执行而无需等待照片上传完成。

最佳答案

因为您在块中使用saveInBackgroundWithBlock:方法,对吗?

https://parse.com/docs/osx/api/Classes/PFFile.html#//api/name/saveInBackgroundWithBlock

Saves the file asynchronously and executes the given block.


如果您确实要等待后台处理的块,则需要按以下方法调用dispatch_group_enterdispatch_group_leave

dispatch_group_enter(groupe);
[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    dispatch_group_leave(groupe);

    ...

}];


顺便说说,


  我希望我的主队列等到照片上传完成


这不是一个好主意。不要阻塞主线程(主队列)。

App Programming Guide for iOS - Performance Tips - Move Work off the Main Thread


  确保限制您在应用程序主线程上执行的工作类型。主线程是您的应用处理触摸事件和其他用户输入的地方。为确保您的应用始终对用户做出响应,您绝对不应使用主线程来执行长时间运行或可能不受限制的任务,例如访问网络的任务。

关于objective-c - GCD等待队列中的所有任务完成,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31290132/

10-14 16:25
查看更多