objective c - NSTimer interval less than sleep command in fired code -
I have a snippet of code that I want to execute repeatedly, but to stop it and start over With capacity To do this, I have used an NSTimer, which I can stop and start as necessary.
Within the snippet, I use a sleep command to wait for some updates (0.3 seconds). The timer is firing in every 0.5 seconds.
Stop the stop and start the functionality, and every 0.3 second firing would be ideal, but clearly do not say that I want to shoot it in every x seconds. 0.5 is completely arbitrary, and just> 0.3 is
If I set the timer to fire every 0.01 seconds, but keep the sleep order in the code executed in 0.3 seconds, then I want the desired behavior? Or the timer will back up all events with unexpected results (for example, after firing I have stopped it)? In this way, I can sleep a variable 0.3 seconds, and whenever I increase it to more than 0.5, it does not have to change the timer.
Or is this a better way of functionality?
I see the biggest problem is that the NSTimer is registered with any thread, its Will consume time.
In most cases for me this is my main thread, which is sleeping inside your main thread Bad thing as far as I can see.
Some alternative designs that may be better for you.
-
Do not sleep Checker selected by the timer check 'Stop me' and exit soon if it is.
-
When you do not cancel it and refuse to refuse NSTimer (note: you
-
-
Make a background thread with its own RunLoop or even its own loop, which operates asleep while sleeping.
They are also in the order that I do them, because the number 1 is the most obvious for me, after No. 2. While the 3 can be viable, very probable atheism Reducing issue, clean shutdown, etc.)
Comments
Post a Comment