-4

I have been facing a issue that my app doesnt run background for more than 10 mins , I have implemented background task which will fetch notifications instantly.

my application background task stops after 10 mins, I have already refereed solutions of this and this , but it doesnt seem helpful

my code is as follows

-(void)methodBGTask :(UIApplication *)application{

    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
        if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
            //create new uiBackgroundTask
            __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
                [application endBackgroundTask:bgTask];
                bgTask = UIBackgroundTaskInvalid;
            }];
            //and create new timer with async call:
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                //run function methodRunAfterBackground
                NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(methodGetNotificatioin) userInfo:nil repeats:YES];
                [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
                [[NSRunLoop currentRunLoop] run];
            });
        }
    }
}


-(void)methodGetNotificatioin{
    //retrieve notifications from service
}

Thanks In advance

Community
  • 1
  • 1
Prasanth S
  • 3,725
  • 9
  • 43
  • 75

3 Answers3

3

This is normal. You are not supposed to run timers in the background. On iOS7 and above, you should be using background fetch mode to fetch data (or do it properly, using push).

Read here for more information on iOS7 background modes.

Note, that on iOS7 and above, background tasks are even shorter (~30 seconds) rather than 10 minutes, so you are even less encouraged to use that API for such work.

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • 1
    @S.P There is no solution. You can implement push, and when the users taps a push notification, the app can open and load all pending data. – Léo Natan Apr 22 '14 at 14:32
1

If I'm not mistaken or not misunderstanding your question, this is expected behaviour. Background tasks are time limited so that one app doesn't run indefinitely and consume resources like battery power and cellular data.

There are different types of background modes, some perform a set task and suspend when complete or timed out, others run periodically.

You're likely looking to implement background fetch, wherein the OS will periodically wake your app and allow it to check for new content and perform a quick data fetch to get the latest data from your server.

Background fetch can be triggered by a push notification that has the "content-available" flag set in its payload. The OS will be selective in scheduling background fetches for apps and will often coalesce them together to be more efficient. The OS will also learn when users run your app and try to schedule background fetches before the time a user opens your app so that the latest data is available.

Jasarien
  • 58,279
  • 31
  • 157
  • 188
0

You should use Push Notifications instead fetching them every 5 minutes. It's will work on the fly and will not drain the battery.

Cy-4AH
  • 4,370
  • 2
  • 15
  • 22