objective c - How do I wait for an asynchronously dispatched block to finish? -
i testing code asynchronous processing using grand central dispatch. testing code looks this:
[object runsomelongoperationanddo:^{ stassert… }]; the tests have wait operation finish. current solution looks this:
__block bool finished = no; [object runsomelongoperationanddo:^{ stassert… finished = yes; }]; while (!finished); which looks bit crude, know better way? expose queue , block calling dispatch_sync:
[object runsomelongoperationanddo:^{ stassert… }]; dispatch_sync(object.queue, ^{}); …but that’s maybe exposing on object.
trying using dispatch_sempahore. should this:
dispatch_semaphore_t sema = dispatch_semaphore_create(0); [object runsomelongoperationanddo:^{ stassert… dispatch_semaphore_signal(sema); }]; dispatch_semaphore_wait(sema, dispatch_time_forever); dispatch_release(sema); this should behave correctly if runsomelongoperationanddo: decides operation isn't long enough merit threading , runs synchronously instead.
Comments
Post a Comment