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

  • Home
  • SEARCH
  • 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 5931335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:40:48+00:00 2026-05-22T14:40:48+00:00

I was reading the following Apple documentation on completion block in dispatch queues, and

  • 0

I was reading the following Apple documentation on completion block in dispatch queues, and I have trouble understanding part of it. The document mentions that "To prevent the queue from being released prematurely, it is critical to retain that queue initially and release it once the completion block has been dispatched." This contradicts my understanding that the block retains all the variables in its closure, which is mentioned in the Block Programming Guide.

What am I missing here? The snippet from the document is pasted below:

A completion block is just another piece of code that you dispatch to a queue at the end of your original task. The calling code typically provides the completion block as a parameter when it starts the task. All the task code has to do is submit the specified block or function to the specified queue when it finishes its work.

Listing 3-4 shows an averaging function implemented using blocks. The last two parameters to the averaging function allow the caller to specify a queue and block to use when reporting the results. After the averaging function computes its value, it passes the results to the specified block and dispatches it to the queue. To prevent the queue from being released prematurely, it is critical to retain that queue initially and release it once the completion block has been dispatched.
Listing 3-4 Executing a completion callback after a task

void average_async(int *data, size_t len, dispatch_queue_t queue, void (^block)(int))
{
   // Retain the queue provided by the user to make
   // sure it does not disappear before the completion
   // block can be called.
   dispatch_retain(queue);

   // Do the work on the default concurrent queue and then
   // call the user-provided block with the results.
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
   int avg = average(data, len);
   dispatch_async(queue, ^{ block(avg);});

   // Release the user-provided queue when done
   dispatch_release(queue);
   });
}
  • 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-05-22T14:40:48+00:00Added an answer on May 22, 2026 at 2:40 pm

    This contradicts my understanding that the block retains all the variables in its closure

    It is not a contradiction, but a misunderstanding. A block retains all objective-c objects that it references. Other object types use their own retain functions instead of a standard one. Therefore, it is impossible for the runtime to know how to retain every variable a block could contain. This is why the queue needs to be retained and released manually.

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

Sidebar

Related Questions

I'm reading the following about UIScrollView from Apple UIScrollView Class Reference Documentation: Because a
Reading a book I have found the following statement: (Object) Behaviors answer either of
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select
I am reading a tutorial that uses the following example (that I'll generalize somewhat):
I have been reading the following text on the Android Developers Site, specifically under
I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx Solving 11 Likely Problems In Your Multithreaded
I was reading the following text from Stanford's Programming Paradigms class , and I
I'm studying the Intel's IA-32 software developer manual. In particular, I'm reading the following
With the following file reading code: using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read,
I am using the following method for reading Csv file content: /// <summary> ///

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.