objective c - GCD wait until all task in queue are finished -
in app got photo upload function , want main queue wait until photo upload done. here's code :
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 annonce //++++++++++++++++++++++++++++++++++++++ if(!_annonce) [kvnprogress updatestatus:@"جاري رفع الصور"]; __block nsinteger numberphototoupload = _photoarray.count - 1; (int = 1; < _photoarray.count; i++) { //check if image asset upload else decrement photo numver because it's 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); // other operation
but didn't worked, program continue execution without waiting photo upload finished.
because using saveinbackgroundwithblock:
method in block, right?
https://parse.com/docs/osx/api/classes/pffile.html#//api/name/saveinbackgroundwithblock:
saves file asynchronously , executes given block.
you need call dispatch_group_enter
, dispatch_group_leave
method following if want wait background-processed block.
dispatch_group_enter(groupe); [imagefile saveinbackgroundwithblock:^(bool succeeded, nserror *error) { dispatch_group_leave(groupe); ... }];
by way,
i want main queue wait until photo upload done
it's not idea. don't block main thread (the main queue).
app programming guide ios - performance tips - move work off main thread
be sure limit type of work on main thread of app. main thread app handles touch events , other user input. ensure app responsive user, should never use main thread perform long-running or potentially unbounded tasks, such tasks access network.
Comments
Post a Comment