Search code examples
iosobjective-cbackground-processgeofencing

Fire 3 synchronous APIs once application wakes up from killed/ suspended state


Geo-Fencing delegate methods didExitRegion and didEnterRegion working for all application states (Foreground / Background / Suspended and Killed state). Once any region gets passed, application need to hit 3 mutually dependent apis synchronously. All is working fine in foreground state, but not in Suspended / Killed state. Don’t know the exact reason of this fail.

One reason may be restricted awake time limit to perform all tasks in such case (killed/ suspended state). I tried beginBackgroundTaskWithExpirationHandler , but its not helping me out.

- (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];
}

Please help, if anyone has any advice about what I am doing wrong here. Thanks in advance


Solution

  • Posting this answer myself, just to clear that above code is sufficient for such task. With above code all needful tasks are working in all application states (foreground/background/killed/suspended).

    My problem was in core data implementation. Once recipe object comes nil, I was skipping further code. It makes me feel like APIs are not firing because of restricted time limit in killed state.

    Hope above code may help others with same issue.