jam
jam

Reputation: 27

Who retains block?

In the following code snippet, there are three types of block. My question is which object references each block? (ARC enabled)

typedef void (^CompletedBlock)(void);

- (void)viewDidLoad {
    [super viewDidLoad];

    [UIView animateWithDuration:<#(NSTimeInterval)#>
                          delay:<#(NSTimeInterval)#>
                        options:<#(UIViewAnimationOptions)#>
                     animations:^{<#code#>}
                     completion:^(BOOL finished) {<#block 1#>}];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
^{<#block 2#>});

    [self myMethod:^{<#block 3#>}];

}

- (void)myMethod:(CompletedBlock)completed
{
    completed();
}

Upvotes: 0

Views: 50

Answers (1)

rob mayoff
rob mayoff

Reputation: 385590

In the case of animateWithDuration:delay:options:animations:completion:, it runs the animations block immediately. No object stores a strong reference to the block after the method returns. It stores the completion block somewhere unspecified. Perhaps it's retained by the current CATransaction.

In the case of dispatch_async, the queue retains the block.

In the case of myMethod:, no object stores a strong (retaining) reference to the block. A strong reference to the block is stored on the stack for the duration of the call.

Upvotes: 2

Related Questions