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 8555025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:01:25+00:00 2026-06-11T15:01:25+00:00

I was wondering if this is the correct way to have nested blocks working

  • 0

I was wondering if this is the correct way to have nested blocks working on the same variable in Objective C without causing any memory problems or crashes with ARC. It starts with a ASIHttpRequest complete block.

MyObject *object = [dataSet objectAtIndex:i];

ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:@"FOO"];

__block MyObject *mutableObject = object;

[request setCompleteBlock:^{

      mutableObject.data = request.responseData;

      __block MyObject *gcdMutableObject = mutableObject;

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{

              [gcdMutableObject doLongComputation];

              dispatch_async(dispatch_get_main_queue(),^{

                     [self updateGUIWithObject:gcdMutableObject];
              }); 

      });

[request startAsynchronous];

My main concern is nesting the dispatch queues and using the __block version of the previous queue to access data. Is what I am doing safe?

  • 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-11T15:01:26+00:00Added an answer on June 11, 2026 at 3:01 pm
    // Under ARC the blocks will automatically retain <object>
    MyObject *object = [dataSet objectAtIndex:i];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:@"FOO"];
    __weak ASIHTTPRequest *weakRequest = request;                        // EDIT
    [request setCompleteBlock:^{
        // <object> is retained by the block.
        // Changing a property of <object> but not <object> itself.
        ASIHTTPRequest *request = weakRequest;                           // EDIT
        if (!request) return;                                            // EDIT
        object.data = request.responseData;    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
            // <object> retained by this block too...
            [object doLongComputation];
            dispatch_async(dispatch_get_main_queue(),^{
                // <object> retained by this block too
                // Note, <self> is also retained...
                // Use the same "weak" trick if you don't want this      // EDIT
                [self updateGUIWithObject:object];
            });
        });
    }];
    [request startAsynchronous];
    

    EDIT

    endy does bring up a valid point (though using __usnafe_unretained should be generally avoided. While I did originally note that both request and self are retained in the original post, I assumed appropriate measures would be taken as necessary. That was not a wrong decision on my part.

    So, there are several ways to break the retain cycle for this request, but using a weak reference is probably the safest and best choice here.

    See the lines marked with // EDIT in the above code.

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

Sidebar

Related Questions

I was just wondering if I'm doing this the correct way. I have 2
I am just learning Objective C and was wondering what the correct way to
I am wondering why this is not working please help me. Here is my
First i was wondering if this is a good idea, essentially i have user
I was just wondering about the correct way to return a reader from a
I was wondering what is the correct way to implement multiple choices in django-models.
I have a multithreading program, and I am wondering which of the way using
This is what I have and I was just wondering if this is the
I am wondering if the shuffle() array function is the correct way to randomize
I'm working with CakePHP 2.x and I was wondering if there is any built-in

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.