Powered by
Powered by
HuntMyIdeas-Inspire You To Make It Happen
Follow
   Me
  • Blogs
  • About Me
  • Home

How to start a long-running task when your app goes to the background in iOS ?

8/18/2018

0 Comments

 
When the user sends an App to background,they moves to an inactive state so that system can send them to a suspended state as soon as possible.If some apps have to complete some tasks or need some extra time ,they can call beginBackgroundTaskWithName:expirationHandler:  or
beginBackgroundTaskWithExpirationHandler: method of UIApplication object to request for additional time
Read:
How to fix some common Xcode Errors
​
Top 5 Simple iOS Interview Questions And Answers

iOS Interview Questions And Answers part 16
​
Top iOS interview Questions And Answers Collection

Top  Interview Questions And Answers in Objective C
The user can avoid suspension of his app temporarily,giving it some extra time to complete the pending task.
Calling either of these methods delays the suspension of your app temporarily, giving it a little extra time to finish its work.
Make sure that you are calling the endBackgroundTask: to inform the system that you have finished your task,now the app can be sent to suspended state.
 
How beginBackgroundTaskWithExpirationHandler: Works

beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler:  generates a unique token that corresponds to a specific task,when called by the user.

Note:
endBackgroundTask: tells the system that the task has been finished.If the user fails to call this method,system can lead to termination of app at any time.

The request to start a background task includes an expiration handler just in case the task takes too long
You can use the below code in applicationDidEnterBackground: method.

- (void)applicationDidEnterBackground:(UIApplication *)application{
UIApplication*    app = [UIApplication sharedApplication];
task = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:task];
        task = UIBackgroundTaskInvalid;
    }];
// Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Do the work associated with the task.
        NSLog(@"Started background task timeremaining = %f", [app backgroundTimeRemaining]);
        if (connectedToNetwork) {
            // do work son...
        }
   [app endBackgroundTask:task];
  task = UIBackgroundTaskInvalid;
    });
}

You can run the task in background for a time limit of 10-20 minutes. It only supports supports versions of iOS iOs 4 
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Enter your email address:

    Delivered by FeedBurner

    RSS Feed

    Archives

    December 2019
    August 2018
    January 2018
    December 2017
    November 2017
    October 2017
    August 2017
    April 2017
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    July 2014
    June 2014


    ​​Categories

    All
    Affiliate Marketing
    Alexa Rank
    Apple's Latest Updates
    Blog Tips
    Cocos2d X
    Cocos2d-x
    Google AdSense Tips
    Grab-some-ultimate-ideas
    How-to-create-a-twinkling-star-background-effect-in-ios
    In-App Purchase Tutorial In Cocos2d-x (C++) Using SDKBOX
    Ios Interview Questions And Answers
    IOS Multiple Choice Questions And Answers
    Objective C
    Online Business
    Python
    Secrets To Effective IPhone App Marketing
    SEO Tips
    Swift Interview Questions
    Swift Tutorials
    Top 10 Articles Objective C
    Top 10 Articles-Objective-C
    Top 10 Popular Movies Ever
    Top 10 Popular Things To Know
    Top 10 Ultimate Issues Xcode
    Top 400 IOS Interview Questions
    Top IOS Sample Projects To Learn From
    Top Secrets To App Marketing
    UICollectionView
    Unity 3D
    Xcode

WELCOME TO HUNTMYIDEAS

Picture
I am Jitendra Gochhayat,an iOS Developer,Designer,blogger,
​from Odisha,India.
I write about ​​​blog tips and,
Make money online Tips,
​Objective C tutorials
SUSCRIBE TO NEWSLETTER

    Subscribe to this blog and receive notifications of new posts by email

Suscribe
Picture
Picture
Picture
Picture
Picture
HuntMyIdeas-2016 Powered By Weebly
Picture
Powered by Create your own unique website with customizable templates.
Powered by
Powered by