Question

I am currently writing an app for the iOS platform which communicates to a server through TCP. The problem is: I want to start an NSThread which runs into an infinite loop that checks that my

- (BOOL)connected

still returns true every 10 seconds, once the connect function has returned successful.

How can I make a timer that runs a function and checks its return value every 10 seconds?

Any help is appreciated, thanks in advance.

Was it helpful?

Solution

Why do not you use NSTimer to run a timer which will run every 10 seconds to check the status of connection?

NSTimer reference is given here. There are many static methods to create a timer. You can use for example:

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

You can use it as

NSTimer * timer = [NSTimer timerWithTimeInterval:10 target:myobject selector:checkConnectionStatus userInfo:nil repeats:YES];

where myobject will be an Objective-C object and its method -checkConnectionStatus or which method you like will check the status of the connection.

OTHER TIPS

You should checkout the Reachability sample code from Apple. I don't know much about TCP, but what you are describing sounds like it is going to burn through the battery quickly. Unless TCP works without waking the radios off course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top