Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy an instance variable into an Objective-C Block

I have the following code:

- (void)downloadPressed:(id)sender {
    [_download startDownloadWithParser:^id(NSData *rawData) {
        NSString* downloadName = [[_download url] lastPathComponent];

        // ... more code
    }];

    [self reloadView];
}

This code is located inside a UITableViewCell, and as we all know, the reuse mechanism should be taken very special note of..

My Question, Clean & Simple:

If I add the following line of code after the block:

_download = nil;

The _download variable inside the block also gets nil'd! I would like it to get a copy instead, how?

like image 745
Mazyod Avatar asked Dec 26 '22 20:12

Mazyod


1 Answers

If you would like your block to get a copy of your ivar, make a local variable from it, and use that local inside your block instead of the ivar:

-(void)downloadPressed:(id)sender {
    MyDownload *tmp = _download; // Make an independent pointer
    [tmp startDownloadWithParser:^id(NSData *rawData) { // Use the local
        NSString* downloadName = [[tmp url] lastPathComponent];
        // ... more code
    }];
    [self reloadView];
}

The block will capture the value of that local variable at the time the block object is created, making all subsequent changes to _download invisible to your block.

like image 55
Sergey Kalinichenko Avatar answered Jan 11 '23 14:01

Sergey Kalinichenko