I am working on an application where i have to call a thread开发者_如何学C repeatedly. In that thread we have to parse an XML and i have to get updated data from that xml. now please guide me how to call that thread repeatedly? if my thread is in working and my code calls it again, then i think it will have a crash on it.
Separate the code into a new method
-(void) thisMethodWillRunAsASeparateThread
{
//Threads need their own pool.
NSAutoreleasePool *pool = [NSAutoreleasePool new];
while (thisThreadShouldRun)
{
// run xml parsing code
}
[pool release];
}
and to start the thread:
[NSThread detachNewThreadSelector:@selector(thisMethodWillRunAsASeparateThread) toTarget:self withObject:nil];
To call any code repeatedly you must NSTimer like:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.10f target:self
selector:@selector(methodName) userInfo:nil repeats:YES];
you should to invalidate the timer after use. while if you wan to execute the code using background thread you must this code but this will not repeat multiple times.
[NSThread detachNewThreadSelector:@selector(methodName:) toTarget:self withObject:objName,nil]];
精彩评论