检查NSTimer选择器是否已完成工作 [英] Check if NSTimer selector has finished working

查看:49
本文介绍了检查NSTimer选择器是否已完成工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每隔3秒,我就会在 NSTimer 中调用一个服务方法.但是,在每次调用该方法之前,我想检查计时器中的上一个是否完整.我已经完成了以下步骤,但未成功.

I am calling a service method in NSTimer after every 3 seconds. However before each time that method is being called, I want to check if previous one in timer is complete or not. I have done following which is not resulting in success.

timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(CallParseOperation) userInfo:nil repeats:YES]; 

CheckOperation :

-(void)CheckOperation{

    if([Data getInstance].kOperation == TRUE)
    {
        [self CallParseOperation];
    }

}

CallParseOperation :

-(void)CallParseOperation{


    [Data getInstance].kOperation = FALSE;
    operationQueue=[[NSOperationQueue alloc] init];
    ParseOperation *obj=[[ParseOperation alloc] initWithMembers:ID];
    NSInvocationOperation *reloadOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(tableReload) object:nil];
    [reloadOp addDependency:obj];
    [operationQueue addOperation:obj];
    [operationQueue addOperation:reloadOp];
    //[operationQueue waitUntilAllOperationsAreFinished];
    [obj release];
    [reloadOp release];

} 

在此实现结束的某个地方,我将此 bool 设置为 TRUE .但这是行不通的.控件只是停留在此计时器内.

Somewhere at the end of this implementation I am setting this bool as TRUE. But this is not working. The control is just stuck inside this timer.

推荐答案

在调用方法 CallParseOperation 之前,应将 kOperation 设置为FALSE,否则它将始终为true并且将每次运行.

You should set the kOperation to FALSE before you call the method CallParseOperation otherwise it will always be true and will run everytime.

与其将计时器设置为重复,不如将其设置为触发一次,然后在解析结束时再次将其设置为关闭.这样,它将在处理后每隔x秒运行一次,而不是每隔x秒运行一次,而不管它是否已完成上一个过程.

Instead of setting your timer to repeat, set it to fire once and then set it off again at the end of your parsing. That way it will run every x seconds AFTER processing rather than every x seconds regardless of whether it has finished the previous process or not.

这篇关于检查NSTimer选择器是否已完成工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆