我的目标是在应用程序在后台运行时每五秒运行一次重复计时器。我尝试了几个想法,但它们似乎都不起作用。
想法1:甚至没有运行一次。
想法2:每五秒运行一次,但我似乎无法停止循环。
想法1:甚至没有运行一次。
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:(5.0/5.0) target:self selector:@selector(check_expiry) userInfo:nil repeats:YES];
}
想法2:每五秒运行一次,但我似乎无法停止循环。
- (void)applicationDidEnterBackground:(UIApplication *)application {
counter = YES;
while (counter) {
sleep(5);
[self check_expiry];
}
// Counter is set to NO in willEnterForeground and didBecomeActive, but this loop continues to run due the sleep();
}
如何使这个循环正常运行?
谢谢!