0

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

Yogendra
  • 1,728
  • 16
  • 28
  • You need to call `endBackgroundUpdateTask` once all of the updates are complete, so call it from the final completion handler – Paulw11 Mar 15 '17 at 11:47
  • @Paulw11 Yes I have called endBackgroundUpdateTask, once all task completed inside callAPITwo: – Yogendra Mar 15 '17 at 11:54
  • Have you refered pushkit, putting app in specific category by taking advance permission from Apple will allow you. – Hasya Mar 15 '17 at 12:02
  • @Hasya No, not tried yet. Could you please tell me, a bit more about it? – Yogendra Mar 15 '17 at 12:03
  • Check this one too http://stackoverflow.com/questions/27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended – Hasya Mar 15 '17 at 12:16

1 Answers1

0

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.

Yogendra
  • 1,728
  • 16
  • 28