Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8528281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:46:00+00:00 2026-06-11T08:46:00+00:00

I have a long running task that I want completed, even if the application

  • 0

I have a long running task that I want completed, even if the application gets pushed to the background. From the documentation I can see that the way to do this is to use the beginBackgroundTaskWithExpirationHandler: feature and asynchronously start a task running as in the following code snippet:

UIApplication* application = [UIApplicationsharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
    // Clean up any unfinished task business by marking where you.
    // stopped or ending the task outright.

    [application endBackgroundTask: bgTask];
    bgTask = 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, preferably in chunks.
         for (int i = 0; i < 3; i++){

        [self doChunkOfWork];
    }

    // end work
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});

Now I understand that the second code block gets executed asynchronously and once completed the application is notified and the tasks is marked as invalid, however, can anyone tell me under what circumstances the first code block gets executed and also how the first and second blocks get associated? Is it simply that because I have started a task and sequentially the next thing I do is call dispatch_async that the two blocks will be associated?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:46:01+00:00Added an answer on June 11, 2026 at 8:46 am

    These blocks are not related in any “magic” way.

    The second block will be queued and run asynchronously, as you state. The first block will only ever be called by the system if:

    1. The application is sent to the background (i.e., the user switches to another app)
    2. Your second block’s [self doChunkOfWork] blocks for more than about 10 minutes (on current iOS versions).

    If -doChunkOfWork blocks, [application endBackgroundTask:bgTask] will never be called, and the system will—rightly!—think you’re still doing work. iOS sets a limit on the amount of time you can work in the background, and it’ll call your expiration handler (the first block) if you exceed this limit.

    Your current implementation for the expiration handler immediately marks the task as finished. At that point, your application would no longer be executing, and if doChunkOfWork is still going, it will be paused. (In fact, all of your application’s threads will be paused.)

    At some future time, the user may switch back to your app. Your threads will then be resumed, and doChunkOfWork will continue executing right where it was.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have long running task that gets called using jquery ajax. i am using
I have an Activity with a button that starts a long running task. I
I have a long running job that updates 1000's of entity groups. I want
I have a long running task that I need to implement on a webpage.
I have a long running Async Task that sends some data to my server
I have a long running, compute and disk intensive task that needs to run
We have an asynchronous task that performs a potentially long-running calculation for an object.
I have a long-running task that performs a series of file operations on mounted
I have a long running Task that uses callbacks to feed data incrementally (rather
I have a long running process that is called via a Nancy Module Get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.