i've seen related questions none seem answer case. want write method work in background. need method call completion callback on same thread / queue used original method call.
- (void)somemethod:(void (^)(bool result))completionhandler { dispatch_queue_t current_queue = // ??? // setup code here dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ bool ok = // result // long running processing here dispatch_async(current_queue, ^{ completionhandler(ok); }); });
what magic incantation needed here completion handler called on same queue or thread call samemethod
? don't want assume main thread. , of course dispatch_get_current_queue
not used.
if through apple docs, there appear 2 patterns.
if assumed completion handler run on main thread, no queue needs provided. example uiview
's animations
methods:
+ (void)animatewithduration:(nstimeinterval)duration animations:(void (^)(void))animations completion:(void (^)(bool finished))completion
otherwise, api asks caller provide queue:
[foo dosomethingwithcompletion:completion targetqueue:yourqueue];
my suggestion follow pattern. if unclear queue completion handler should called, caller should supply explicitly parameter.