Objective C ASIHTTPRequest nested GCD block in complete block

Posted by T.Leavy on Stack Overflow See other posts from Stack Overflow or by T.Leavy
Published on 2012-09-19T20:12:52Z Indexed on 2012/09/19 21:37 UTC
Read the original article Hit count: 605

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?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about automatic-ref-counting