Q:

objective-c using blocks with recursion

I am coding an iphone app. And when I used blocks with recursion in Objective-C,
 I got a warning message of  

Capturing addImageToUploadEntity strongly in this block is likely to lead to a retain cycle.

I created a block called addImageToUploadEntity is used to call an async function [self.submissionEntity addImageData: toImageAlbumType: finish^{}] one by one. When it hits the base case (index <= 0), it reload the table View and return.
    __block void (^addImageToUploadEntity)(NSUInteger) = ^(NSUInteger index){
        if (index <= 0)
        {
            [self.tableView reloadData];
            return;
        }

        [self.submissionEntity addImageData:[imagesPathArray objectAtIndex:index] toImageAlbumType:_currentOperatingImageAlbumType finish:^{
            // recursively call the block with index+1
            addImageToUploadEntity(index-1);
        }];
    };
    addImageToUploadEntity(5);

After I have researched a bit, some suggested to call the function addImageToUploadEntity() with weak type inside the block. So I rewrote this part and tried. Then, I got an error of 'bad access'.
What should I do in order to improve the code?
And with this block, does it have any memory leak risk?
    __block void (^addImageToUploadEntity)(NSUInteger);
    void (^__block __weak weakaddImageToUploadEntity)(NSUInteger);

    weakaddImageToUploadEntity = addImageToUploadEntity = ^(NSUInteger index){
        if (index <= 0)
        {
            [self.tableView reloadData];
            return;
        }

        [self.submissionEntity addImageData:[imagesPathArray objectAtIndex:index] toImageAlbumType:_currentOperatingImageAlbumType finish:^{
            // recursively call the block with index+1
            weakaddImageToUploadEntity(index+1);
        }];
    };
    addImageToUploadEntity(5);

A:

The asynchronous completion handler for addImageData:toImageAlbumType:finish:  needs to capture a strong reference to your block.  Convert the weak reference back into a strong reference, and use the strong reference inside of the asynchronous completion handler:
typedef void (^AddImageBlock)(NSUInteger);

__block __weak AddImageBlock weakAddImage = nil;

AddImageBlock addImage = ^(NSUInteger index) {
    if (index <= 0)
    {
        [self.tableView reloadData];
        return;
    }

    AddImageBlock strongAddImage = weakAddImage;
    [self.submissionEntity addImageData:[imagesPathArray objectAtIndex:index] toImageAlbumType:_currentOperatingImageAlbumType finish:^{
        // recursively call the block with index+1
        strongAddImage(index+1);
    }];
};

weakAddImage = addImage;
addImage(5);

The AddImageBlock will kept alive until the last asynchronous method handler is executed and released.

