2012-06-20 38 views
0

调用两次在“-applicationDidEnterBackground”的方法我beginBackgroundTaskWithExpirationHandler代码块,我的NSTimer实现如下:选择方法beginBackgroundTaskWithExpirationHandler

UIBackgroundTaskIdentifier taskId = [application beginBackgroundTaskWithExpirationHandler:^{ 
     timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(print:) userInfo:nil repeats:NO]; 
    }]; 

而且Eventhough其repeats:NO,“print'method的选择是叫两次,我可以通过NSLog看到。

这个计时器并没有从applicationDidEnterBackground以外的任何地方被调用。

回答

1

我向你保证,没有什么不妥:

timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(print:) userInfo:nil repeats:NO]; 

你的“打印”西港岛线的方法不会被调用两次。你的问题与别的东西有关。 另一件事,一个0.0时间间隔的计时器是毫无意义的,你不同意吗?

+0

嗯..是的,你是对的。我不知道我是什么。所以,我jst用'print'方法的代码块去除了定时器,并且它的行为方式相同。所以,这个行为与计时器有关。但似乎'beginBackgroundTaskWithExpirationHandler'的代码块正在执行两次。其实,我在我的项目中使用CLLocation管理器,可以这样做吗? – lasata

+0

我不能确定没有你的代码。发布你做了什么,也许我可以帮助。 –