问题描述
我一直面临一个问题,我的应用程序运行后台超过10分钟,我已经实现了后台任务,它将立即获取通知。
I have been facing a issue that my app doesnt run background for more than 10 mins , I have implemented background task which will fetch notifications instantly.
我的应用程序背景任务在10分钟后停止,我已经审查了和,但它似乎没有帮助
my application background task stops after 10 mins, I have already refereed solutions of this and this , but it doesnt seem helpful
我的代码如下
-(void)methodBGTask :(UIApplication *)application{
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
//create new uiBackgroundTask
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
//and create new timer with async call:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//run function methodRunAfterBackground
NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(methodGetNotificatioin) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
});
}
}
}
-(void)methodGetNotificatioin{
//retrieve notifications from service
}
提前谢谢
推荐答案
这是正常的。你不应该在后台运行计时器。在iOS7及更高版本中,您应该使用后台获取模式来获取数据(或使用推送正确执行)。
This is normal. You are not supposed to run timers in the background. On iOS7 and above, you should be using background fetch mode to fetch data (or do it properly, using push).
读取了解有关iOS7背景模式的更多信息。
Read here for more information on iOS7 background modes.
注意,在iOS7及更高版本上,后台任务甚至更短(约30秒)而不是10分钟,因此您甚至不太鼓励使用该API进行此类工作。
Note, that on iOS7 and above, background tasks are even shorter (~30 seconds) rather than 10 minutes, so you are even less encouraged to use that API for such work.
这篇关于我的iphone应用程序后台进程在10分钟后终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!