I need to ping a server at fixed intervals. I am currently using the following:
[NSTimer scheduledTimerWithTimeInterval:5.0f
target:[Socket getInstance]
selector:@selector(sendHeartBeats)
userInfo:nil
repeats:YES];
This calls function sendHeartBeats
at an interval of 5 sec.
Do i need to call this on a separate thread so that my main thread will not be affected?
NSTimer
s, as well as the related NSRunLoop
, do not affect (nor are aware of) the threading behavior of your process. Both just use the current thread.
This means that you have to care about threads on your own. NSTimer
, in conjunction with NSRunLoop
give you the opportunity to schedule timed tasks on a given thread.
You can use a timer on the main thread or start a new thread, add a runloop to it and start a timer on that background thread.
Anyway, when using threads, you have to be aware of thread safety issues. In this case this means making the Socket
class (singleton?) thread safe because it is probably used elsewhere in your app.