应用程序从被杀死/暂停状态

时间:2017-03-15 11:18:34

标签: ios objective-c background-process geofencing

Geo-Fencing委托方法 didExitRegion didEnterRegion 适用于所有应用程序状态(前景/背景/暂停和已终止状态)。一旦任何区域通过,应用程序需要同步命中3个相互依赖的apis。一切都在前景状态下正常工作,但未处于暂停/终止状态。不知道这次失败的确切原因。

在这种情况下(死亡/暂停状态)执行所有任务的一个原因可能是限制唤醒时间限制。我试过 beginBackgroundTaskWithExpirationHandler ,但它并没有帮助我。

- (void) beginBackgroundUpdateTask{

    _backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}

- (void) endBackgroundUpdateTask{

    [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
    _backgroundTask = UIBackgroundTaskInvalid;
}


- (void)locationManager:(CLLocationManager *)manager
         didEnterRegion:(nonnull CLRegion *)region
{
    [kSharedAppDelegate beginBackgroundUpdateTask];

    NSString *locId = [region.identifier substringFromIndex:1];
    Recipe *recipe = [Recipe getSelectedReturnRecipeForLocationId:locId];
    if(recipe)
    {
        [self  callAPIOne:^(NSDictionary *dictResponse) {

          [self callAPITwo:params forAbc:NO];

        } withFailed:^(NSDictionary *dictResponse) {
                 [kSharedAppDelegate endBackgroundUpdateTask];
        } showLoader:NO];
    }
    else
        [kSharedAppDelegate endBackgroundUpdateTask];
}

请帮助,如果有人对我在这里做错了什么有任何建议。提前致谢

1 个答案:

答案 0 :(得分:0)

自己发布此答案,只是为了清除 以上代码足以完成此类任务。使用上面的代码,所有需要的任务都在所有应用程序状态下工作(前景/后台/被杀/暂停)。

我的问题在于核心数据实施。一旦配方对象变为零,我就会跳过更多代码。由于受到限制的时间限制,我觉得API没有被触发。

希望上面的代码可以帮助其他人解决同样的问题。