views:

313

answers:

2

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

+1  A: 

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

mihirpmehta
A: 

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
}
Nava Carmon
i have everything set i mean reachability etc... only thing is that i want to revoke the method after every 5,10 secs or mints let say
Ch Sab
see again the answer, i added there some sample code
Nava Carmon
thanks a lot dear
Ch Sab