开发者

How to call NSThread repeatedly?

开发者 https://www.devze.com 2023-04-04 11:11 出处:网络
I am working on an application where i have to call a thread开发者_如何学C repeatedly. In that thread we have to parse an XMLand i have to get updated datafrom that xml. now please guide me how to cal

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]];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号