Objective c 应用程序标识背景时运行重复计时器
我的目标是,当且仅当应用程序在后台运行时,每五秒钟运行一次重复计时器。我试过几个主意,但似乎不管用 想法1:一次也不运行。Objective c 应用程序标识背景时运行重复计时器,objective-c,ios,xcode4,nstimer,Objective C,Ios,Xcode4,Nstimer,我的目标是,当且仅当应用程序在后台运行时,每五秒钟运行一次重复计时器。我试过几个主意,但似乎不管用 想法1:一次也不运行。 - (void)applicationDidEnterBackground:(UIApplication *)application { [NSTimer scheduledTimerWithTimeInterval:(5.0/5.0) target:self selector:@selector(check_expiry) userInfo:nil repeat
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:(5.0/5.0) target:self selector:@selector(check_expiry) userInfo:nil repeats:YES];
}
- (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();
}
想法2:每五秒钟运行一次,但我似乎无法停止循环。
- (void)applicationDidEnterBackground:(UIApplication *)application {
[NSTimer scheduledTimerWithTimeInterval:(5.0/5.0) target:self selector:@selector(check_expiry) userInfo:nil repeats:YES];
}
- (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();
}
如何使此循环正常运行
谢谢 当应用程序在iOS中“进入后台”时,与正常操作系统不同,它会继续运行。应用程序进入挂起状态。它不会一直运行;只保留应用程序状态,即使这样也不能保证——如果设备内存不足,iOS会很高兴地终止应用程序,将内存提供给活动应用程序。如果您试图在ApplicationIdentinterBackground:
中进行阻止,就像您在sleep()
中所做的那样,iOS将简单地终止您的应用程序,因为它没有立即从该方法返回
如果您的应用程序被配置为后台处理GPS事件、VOIP等,则会定期唤醒,但滥用这些功能只是为了让您的应用程序运行将阻止您获得应用程序商店的批准
所有这些都包含在中。对于任何想要解决问题的人,我只是创建了一个系统,在
ApplicationIdentinterBackground:
之后的某个日期调度计时器,并在编辑/删除计时器时对其进行更改/取消。有关计时器调度的信息存储在本地字典中