ios7 - Problems With Using Dispatch Queue in Objective C -


i trying use dispatch queue can wait 1 portion of code finish before continuing onto second portion. below code, code within dispatch_sync execute , complete before code in dispatch_barrier_async. have looked around trying figure out why doesn't work no avail. appreciated.

__block dispatch_queue_t queue; queue = dispatch_queue_create("com.example.myqueueformaps", dispatch_queue_concurrent);  dispatch_sync(queue, ^{      for(xyztodoitem* item in todoitems){          mklocalsearchrequest *request = [[mklocalsearchrequest alloc] init];         request.naturallanguagequery = item.itemname;         // somehow deal radius         mkcoordinatespan span = mkcoordinatespanmake(0.1, 0.1);         request.region = mkcoordinateregionmake(currentloc.coordinate, span);          mklocalsearch *search = [[mklocalsearch alloc]initwithrequest:request];         [search startwithcompletionhandler:^(mklocalsearchresponse *response, nserror *error){             int = 0;             double minimum = infinity;             mkmapitem *closest;              (mkmapitem *item in response.mapitems) {                  uilabel *mylabel = [[uilabel alloc] initwithframe:cgrectmake(10, 50 + 20*i, 300, 200)];                 mylabel.numberoflines = 1;                  nsstring *mystring = [nsstring stringwithformat:@"%@ %@", item.name, item.phonenumber];                 mylabel.text = mystring;                  cllocation *loc = item.placemark.location;                 cllocationdistance dist = [currentloc distancefromlocation:loc];                  if (dist < minimum) {                     minimum = dist;                     closest = item;                 }                 //nslog(@"%@", closest.name);                 //nslog(@"dist %f", minimum);             }              if (minimum < item.radius) {                 nslog(@"match %@", closest.name);                 item.closematch = closest;                 item.match = true;             }             else {                 item.match = false;                 item.closematch = nil;             }         }];     } });  dispatch_barrier_async(queue, ^{     [findmatches notifynearbytasks]; }); 

the code in dispatch_sync() block should indeed finish executing before barrier block. same isn't true code in completion block pass -[mklocalsearch startwithcompletionhandler:], however. -startwithcompletionhandler: asynchronous method, , there's chance search won't complete before end of block in start it.

note completion block runs on main thread -- the docs:

the provided completion handler executed on app’s main thread.

that is, completion block won't scheduled on queue, on main dispatch queue instead. so, if completion block scheduled before synchronous block completes, scheduled on different dispatch queue , wouldn't prevent barrier block starting.

it looks goal delay call [findmatches notifynearbytasks] until searches have completed. 1 way might add each search list, , have completion block check remove search list, check size of list, , make appropriate call when list empty.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -