我每隔3秒就在NSTimer
调用一个服务方法。但是在每次调用该方法之前,我想检查计时器中的前一个是否完成。我做了以下并没有取得成功。
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
。但这不起作用。控件刚停留在此计时器内。
答案 0 :(得分:3)
在调用方法kOperation
之前,您应该将CallParseOperation
设置为FALSE,否则它将始终为true并且每次都会运行。
不要将计时器设置为重复,而是将其设置为触发一次,然后在解析结束时再将其设置为关闭。这样它将在处理后每隔x秒运行一次,而不是每x秒运行一次,无论它是否完成了前一个过程。