1

I am trying to call performFecthWithCompletionHandler using background fetch feature of ios7. For that I have set 180seconds of time interval.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    NSLog(@"Did finish launching");
    NSTimeInterval timeInterval = 180;
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:timeInterval];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    return YES;
}

I have gone through many threads where it suggests for Xcode -> Debug -> Simulate Background fetch. Which is working fine.

But I want the app to call this method automatically after timeInterval finishes. I have tried with running the app without connecting to Xcode. But it does not call after 180seconds. It takes some more time to getting it called.

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  NSLog(@"########### Received Backgroudn Fetch ###########");

    //Increase Badge Number
    [UIApplication sharedApplication].applicationIconBadgeNumber++;

    UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController;
    NSLog(@"fetch");
    id topViewController = navigationController.topViewController;
    if ([topViewController isKindOfClass:[ViewController class]]) {
        [(ViewController*)topViewController insertNewObjectForFetchWithCompletionHandler:completionHandler];
    } else {
        NSLog(@"Not the right class %@.", [topViewController class]);
        completionHandler(UIBackgroundFetchResultFailed);
    }
}

Anything I am missing here? Or what makes it not get called

Heena
  • 2,348
  • 3
  • 32
  • 58

1 Answers1

3

You do not get to control when the operating system allows your application to "wake up" in the background and perform the background fetch. The OS decides, based on many different factors, how often your application should be allowed to perform the fetch. I've seen this vary between different devices with the same app hours sometimes, depending how long the application has been on the device, how many background fetches it has completed and how quickly/efficiently it completes them, etc.

When you set the minimum background fetch time interval, that does not tell the OS how often to perform the fetch, it tells the OS that it SHOULD NOT perform the background fetch more often than that. The minimum background fetch time interval:

Specifies the minimum amount of time that must elapse between background fetch operations.

https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/setMinimumBackgroundFetchInterval:

I would highly recommend reading the documentation and anything related to iOS7 multitasking to get a better understanding of how this works.

Mike
  • 9,765
  • 5
  • 34
  • 59