Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Question: Chap 15 main thread for background task

Hi,
for the last code snippet on page 542 in Chap 15 concerning the request for background time I see that certain UIKit components are being used to carry out work (smileyView and segment control) in a thread that is not the main thread. I guess this is because the app is already in the background and therefore off the main thread so it is safe to do some final messaging on those GUI elements from a background thread?

Comments

  • Good catch, the calls to UIKit classes in this example should have been done back on the main thread.

    It is never "safe" to call UIKit classes from any thread but the main thread, unless the documentation explicitly says it is thread safe. The reason it works is because threading is tricky, sometimes it works, sometimes it do not. Could break the third time you run it, after updating to the next iOS version, or only if a GPS locatin is being fetched at the same time, for example.

    It is this indeterministic behaviors that makes bugs caused by thread issues hard to debug, and all the more a reason to change this sample code to only call UIKit classes on the main thread.

    // Fredrik

     
  • cool, thanks for clearing that up!
Sign In or Register to comment.