Reputation: 33048
Let's assume we have simple UI and for whatever reason, every 5 seconds a UILabel needs to be updated.
I can now either setup an NSTimer which does the job, or create a separate thread which runs endlessly, sleeps 5 seconds, does its thing, sleeps again and so on.
Which one would I choose to go for? What are the differences?
Upvotes: 1
Views: 537
Reputation: 86651
Use NSTimer
.
Why?
-performSelectorOnMainThread:withObject:waitUntilDone:
to update the UI.If the calculations for the label take a while (don't just assume they do, try it the simple way first and see), have your timer kick off an NSOperation to do the calculations and then have the NSOperations call -performSelectorOnMainThread:withObject:waitUntilDone:
when it has finished.
Upvotes: 1
Reputation: 29833
The UI can only be updated on the main thread of any program. If you want to do calculations for the label in a separate thread, that's perfectly fine, but the code to update your UI will have to operate on the main thread (or, you can use performSelectorOnMainThread:withObject:waitUntilDone:
to call setText:
on your label and have it operate correctly).
Upvotes: 1
Reputation: 2720
You have to do UI operations on the main thread anyway, so unless your doing some processing prior to setting the label there would be no benefit of using another thread.
Upvotes: 1