Reputation: 6323
In other words, if I have a process that continuously runs, but users can change parameters on the GUI that effect the process operation characteristics, where is a better place to put the process, in a NSThread
or NSTimer
?
Upvotes: 1
Views: 918
Reputation: 55573
While NSThread
and NSTimer
are two separate things for different needs, lets compare the two functions:
Using NSThread
:
-(void) doSomethingEverySecond {
__block int cumValue = 0; // cumulative value
__block void(^execBlock)() = ^{
while (1)
{
@try
{
// some code here that might either A: call continue to continue the loop,
// or B: throw an exception.
cumValue++;
NSLog(@"Cumulative Value is: %i", cumValue);
if (cumValue == 5)
return;
}
@finally
{
[NSThread sleepForTimeInterval:1];
}
}
};
[NSThread detachNewThreadSelector:@selector(invoke) toTarget:[execBlock copy] withObject:nil];
}
Using NSTimer
:
-(void) doSomethingEverySecond {
__block NSTimer *timer = nil;
__block int cumValue = 0;
__block void (^execBlock)() = ^{
cumValue++;
NSLog(@"Cumulative Value is: %i", cumValue);
if (cumValue == 5)
[timer invalidate];
};
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[execBlock copy] selector:@selector(invoke) userInfo:nil repeats:YES];
}
Now, if we want to something only once, NSThread
is the way to go, as shown in the following:
-(void) doSomethingOnce {
__block void (^execBlock)() = ^{
NSLog(@"Doing something that could take a LONG time!");
};
[NSThread detachNewThreadSelector:@selector(invoke) toTarget:[execBlock copy] withObject:nil];
}
Now, for the NSTimer
variant:
-(void) doSomethingOnce {
__block void (^execBlock)() = ^{
NSLog(@"Doing something that could take a LONG time!");
};
[NSTimer scheduledTimerWithTimeInterval:0 target:[execBlock copy] selector:@selector(invoke) userInfo:nil repeats:NO];
}
The reason for this is that we have complete control over the thread while using a NSThread
, but if using a NSTimer
, than we are executing inside a NSRunLoop
which may freeze the UI if any heavy lifting is done inside. THAT is the advantage of a NSThread
over a NSTimer
.
You are also guaranteed that a NSThread
that is detached is executed immediately, with a NSTimer
, which is based on NSRunLoop, cannot, as it may or may not be able to execute immediately.
There is a 3rd alternative (well technically a fourth too, pthread
s, but I will ignore that for now), GCD
, but I would suggest you RTFM on that, as it's too broad of a topic to cover in this post.
Upvotes: 1
Reputation: 23634
NSThread and NSTimer are not mutually exclusive or replacements for one another. NSThread allows you to control a thread of execution and NSTimer is just that, a timer.
I assume you mean running an NSTimer on a background thread rather than on the main thread? That is generally a good idea so that the timer has less potential to be delayed by things occurring on your main thread (such as user interaction with the application).
You should read Apple's Threading Programming Guide.
Upvotes: 1