ios - How do I nest blocks so they end at the same time -
how turn method block won't complete until 'request startwithcompletionhandler' block completes it's request , code in own block?
i have series of blocks need call in order require code sent requests. trying find clean way accomplish task.
+(void)???????? ^block() { fbrequest *request = [fbrequest requestforgraphpath:kfacebookquerymekey]; [request startwithcompletionhandler:^(fbrequestconnection *connection, id result, nserror *error) { .... }]; }
added info: yes, want outer function wait until inner function complete
thanks.
it depends on mean. given example code, seems want function call block when nested block completes. you'd want this:
+ (void)domythingwithblock:(void(^)(void))block { fbrequest *request = [fbrequest requestforgraphpath:kfacebookquerymekey]; [request startwithcompletionhandler:^(fbrequestconnection *connection, id result, nserror *error) { // whatever want connection ... if (block) block(); }]; }
on other hand, maybe want outer function wait nested block complete before returns. in case, it's bit more complicated. need use lock wait block complete, this:
#define kmythingnotdone 0 #define kmythingdone 1 + (void)domythingandwait { nsconditionlock *lock = [[nsconditionlock alloc] initwithcondition:kmythingnotdone]; fbrequest *request = [fbrequest requestforgraphpath:kfacebookquerymekey]; [request startwithcompletionhandler:^(fbrequestconnection *connection, id result, nserror *error) { // whatever want connection ... [lock lock]; [lock unlockwithcondition:kmythingdone]; }]; [lock lockwhencondition:kmythingdone]; [lock unlock]; }
be warned - don't want on main thread, or you'll lock user interface. have sure request isn't going wait happen on thread call from, otherwise you'll end deadlock.
Comments
Post a Comment