IDev
IDev

Reputation: 1217

nsthread in iphone xcode

Good Day!

i want to use nsthreads in a project of xcode that will call a function which is for the network access and will check that if the network is there or not, so i need to have a thread which will execute after lets say 1 minutes to check the connectivity. and will continue run unless the app is closed.

[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];

startTheBackgroundJob

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// wait for 3 seconds before starting the thread, you don't have to do that. This is just an example how to stop the NSThread for some time
[NSThread sleepForTimeInterval:5];
//[self performSelectorInBackground:@selector(checkNet) withObject:nil];
[self performSelectorOnMainThread:@selector(checkNet) withObject:nil waitUntilDone:YES];
[pool release];

it works only for the first time but not any other, i mean only 1 loop it makes

can somebody help me in this regard.

Thanks

Upvotes: 0

Views: 3353

Answers (3)

sinh99
sinh99

Reputation: 3979

Threads are especially useful when you need to perform a lengthy task, but don’t want it to block the execution of the rest of the application. In particular, you can use threads to avoid blocking the main thread of the application.

To understand more.... find nice simple and short source code from http://www.xprogress.com/post-36-threading-tutorial-using-nsthread-in-iphone-sdk-objective-c/

Upvotes: 1

Nava Carmon
Nava Carmon

Reputation: 4533

I'd suggest a combination of NSTimer, that fires each 5-10 seconds + a Reachability interface, which checks for you the network status. No need for a thread. For using the Reachability check Apple's example.

Put this call in viewDidLoad

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];

-(void) onTimer:(NSTimer *)theTimer 
{
    //Here perform reachability checks
}

Upvotes: 0

Mihir Mehta
Mihir Mehta

Reputation: 13843

You can use NStimer instead... With Property Repeated set to YES

Upvotes: 1

Related Questions