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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With