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:

  1. ui updates
  2. delegate callbacks ui code
  3. handling timers
  4. touch handling

etc... etc...


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -