我正在使用重要的位置更改(SLC)来跟踪用户在旅途中的所有移动,但是一旦用户停止,则不会引发进一步的更新,并且应用程序没有机会提交新位置(当用户保持相同时)区域,但没有SLC).
为了涵盖这种情况,我开始使用后台提取来定期在后台发送更新位置,即使没有SLC).这里的问题是背景提取常常工作(自从我在另一个iOS 7.x应用程序中使用以来每20-30分钟)但是现在使用iOS8 / iOS9我每天只能获得一次,这是不可接受的在我的情况下.我已经执行了大量测试,开发了简单的后台获取应用程序,它在获取时添加本地通知.没有运气迫使它更频繁地工作.
这是我的AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; return YES; } - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [[UIApplication sharedApplication] cancelAllLocalNotifications]; UILocalNotification *localNotification = [[UILocalNotification alloc] init]; NSDate *now = [NSDate date]; localNotification.fireDate = now; localNotification.alertBody = [NSString stringWithFormat:@"Background fetch!"]; localNotification.soundName = UILocalNotificationDefaultSoundName; NSInteger number = [UIApplication sharedApplication].applicationIconBadgeNumber; number++; localNotification.applicationIconBadgeNumber = number; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; completionHandler(UIBackgroundFetchResultNewData); }
这里所做的就是在每次后台提取时添加本地通知.我总是用UIBackgroundFetchResultNewData完成后台执行.
解决方法
>你花在处理程序上的时间
>结果(NoData,NewData)
>错误处理(如果您编码崩溃,您将不太可能启动
>超时(你的代码执行可能被iOS中断)
>用电量
>与结果相关的网络活动(当你说你有NewData时,你必须做一个网络请求,否则你的获取可能会在一天左右的下一次执行.
Apps that download small amounts of content quickly,and accurately
reflect when they had content available to download,are more likely
to receive execution time in the future than apps that take a long
time to download their content or that claim content was available
but then do not download anything.
最后一项在我的案例中是至关重要的,因为出于测试目的,我声明了提取并且从未尝试过下载任何内容.一旦我开始在处理程序中使用网络,后台提取将继续按预期每15-30分钟工作一次.