Reputation: 397
I use sleepForTimeInterval
. My code
- (void)runningInBackground
{
while (1) {
[NSThread sleepForTimeInterval:waitInterval];
[self getInterval];
}
}
How can i change interval if sleepForTimeInterval
is not finished?
Upvotes: 0
Views: 234
Reputation: 299265
There are almost no cases where sleepForTimeInterval:
is correct iOS code. iOS performs these kinds of things with NSTimer
, NSOperation
or GCD queues. If you find yourself calling NSThread
, you are almost certainly in the wrong part of the framework.
Without knowing the details of your problem, the tool you probably want is an NSTimer
. They're simple to use, and solving this kind of problem is easy with them. You just invalidate the timer and create a new one when you want to change the interval. You don't need to break out of a sleep.
But you should always ask yourself if you could turn a polling (interval-based) solution into an event-driven solution. What are you doing when you wake up? If you're usually just checking something and going back to sleep, that's very bad for the battery. iOS has good solutions for making those things event driven in most cases (so you just get called when the thing you want occurs without polling).
Upvotes: 1
Reputation: 2579
You can not. No run loop processing occurs while the thread is blocked.
Upvotes: 1