iphone - Invoke model method with block that will run on the main thread -
one of central tenets of architecture of latest app i'm going call methods on app's model async , accept failure , success scenario blocks.
i.e., ui calls model method 2 blocks, 1 success , 1 failure.
this great because context of original call retained, however, block called on background thread. there anyway of calling block on main thread??
hopefully have explianed ok, if not, basically, model methods async, return , create new thread on run op. once op returns invoke block postprocess returned data, need call block success scenario defined called inside ui. however, success , failure scenario blocks defined in ui should called in main thread because need interact ui elements should done on main thread believe.
many
something you're after:
- (void) dosomethingwhichtakesageswitharg: (id) thearg resulthandler: (void (^)(bool, id, nserror *)) handler { // run in background, on default priority queue dispatch_async( dispatch_get_global_queue(0, 0), ^{ id somevar = [thearg computesomething]; nserror * anerror = nil; [somevar transmutesomehowusing: self error: &anerror]; // call result handler block on main queue (i.e. main thread) dispatch_async( dispatch_get_main_queue(), ^{ // running synchronously on main thread -- call handler handler( (error == nil), thearg, anerror ); }); }); }
Comments
Post a Comment