我的目標是當且僅當應用程序在後臺運行時每五秒運行一次重複計時器。我已經嘗試了幾個想法,但他們似乎沒有工作。當applicationDidEnterBackground運行重複計時器
想法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();
}
如何讓這個循環正常運行?
謝謝!