Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to set NetworkActivityIndicatorVisible value in secondary thread?

I wonder if AppDelegate is thread safe? I currently have an operation running networking tasks on the secondary thread, when the task begins, I would like to set NetworkActivityIndicatorVisible to YES, and when the task is done, set it to NO. Do I have to always call it in the main thread, or I can do it in the current run loop thread?

Thanks

like image 242
Jason Avatar asked Aug 03 '12 15:08

Jason


1 Answers

In general, UIKit is not thread safe. While you may be able to "get away" with some things, you should always do UIKit stuff on the main thread. There are a few, well documented, exceptions.

The pattern for doing this from a background thread is simple.

dispatch_async(dispatch_get_main_queue(), ^{
    // Put any code you want to execute in the main thread here.
});

The code inside the block you pass to dispatch_async will be scheduled to run in the main run loop.

like image 139
Jody Hagins Avatar answered Nov 14 '22 07:11

Jody Hagins