LuckyLuke
LuckyLuke

Reputation: 49057

NSTimer and performing a selector on the main thread

Is it possible to run a selector which is invoked by the nstimer on the main thread?

The NSTimer is spawned in it's own thread.

My structure is that a thread invokes a method with a nstimer, the nstimer invokes a method that do some updates, but I need these updates to happen on the main thread. What is the solution? Add another method and say performOnMainThread?

Upvotes: 3

Views: 1032

Answers (1)

Bogatyr
Bogatyr

Reputation: 19323

Yes, from your timer routine just call use performOnMainThread. From the docs:

Timers work in conjunction with run loops. To use a timer effectively, you should be aware of how run loops operate—see NSRunLoop and Threading Programming Guide. Note in particular that run loops retain their timers, so you can release a timer after you have added it to a run loop.

also a caveat about invalidating repeating timers:

for a repeating timer, you must invalidate the timer object yourself by calling its invalidate method. Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the invalidate method from the same thread on which the timer was installed

Upvotes: 4

Related Questions