0

In our App we use background fetch to get user data (Location, Connections etc) and after 1 hour in background the App dying and we have not any updates. Why is this happening and how we can get results after 1 hour?

This is our code:

-- AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    ...
}

- (void)application: (UIApplication *)application performFetchWithCompletionHandler:(void(^) (UIBackgroundFetchResult))completionHandler {
    NSDictionary *fetchData = ...
    NSString * baseUrl = @"http://......";
    for(NSString *fetchKey in fetchData) {
        baseUrl = [baseUrl stringByAppendingString:[NSString stringWithFormat:@"?%@=%@", fetchKey, [fetchData objectForKey:fetchKey]]];
    }

    NSURLSession *session = [self backgroundSession];
    NSURL *url = [[NSURL alloc] initWithString:baseUrl];
    [[session downloadTaskWithURL:url] resume];

    ...
}

- (NSURLSession *)backgroundSession
{
    static NSURLSession *session = nil;
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.username.app"];
    sessionConfiguration.sessionSendsLaunchEvents = YES;
    sessionConfiguration.discretionary = YES;

    return session;
}

Thank you!

  • Background fetch has limited time resource to complete a task. check this link for further detail. http://stackoverflow.com/questions/25210848/ios-background-fetch-time-limit-crash. – Muneeba Jan 22 '16 at 11:36
  • @Muneeba But we use background fetch each 15 min and we have stable results. We start losing results after 1 hour of background work. – Jagga-Jagga Jan 22 '16 at 14:02
  • May be you ran out of time or your task is taking too much time that OS is terminating it. Also apple don't allow longer task execution in background. You need to go for alternative.You can get the updated data at next time user launched the app. or when app become active. – Muneeba Jan 25 '16 at 04:57
  • Check this link for possible App termination reasons https://blog.newrelic.com/2016/01/13/ios9-background-execution/ – Muneeba Jan 25 '16 at 05:05

0 Answers0