ios - Completion block thread -
i have piece of code:
[[fbcontroller sharedcontroller] getuserdetailswithcompletionblock:^(nsdictionary *details) { // updating ui elements }];
i don't understand thing: when block
fired, secondary thread
still running. isn't more correct completion
of block
should executed on main thread
automatically?
i know wrong , need couple of explanations.
the facebook sdk documentation should give more details, in general well-behaved sdk call completion blocks on same thread sdk called from. long-running or asynchronous operations sdk may perform should operate on separate thread, visible sdk. whether or not separate thread still running or not, implementation detail of sdk - , shouldn't care client code perspective.
you can visualise this:
client code (main thread) : [request]--[response]-[continue thread]-------[completion block] v ^ ^ sdk code (main thread) : [immediate operations] | v | sdk code (private thread) : [long running / asynchronous operations]----[finished]
in specific example posted, there's no 'response' getuserdetailswithcompletionblock
method, thread carries on usual.
the missing piece jigsaw puzzle might - "how completion block executed on main thread". comes down runloop system. main thread isn't owned , operated code, it's behind scenes. there's main runloop periodically looks things do. when there's do, operates somethings on main thread sequentially. when somethings have finished, goes looking else do. sdk adds completion block main runloop, next time fires, block there waiting executed.
other things runloop might doing are:
- ui updates
- delegate callbacks ui code
- handling timers
- touch handling
etc... etc...
Comments
Post a Comment