So I was reading How do I avoid capturing self in blocks when implementing an API? about how memory management works in regards to referencing self within a completion block and that got me thinking: will the following cause a retain cycle?
Pseudocode:
[AFnetworking requestGet:@"http://www.website.com" completionBlock:(^)(RequestObj* request, NSError* error){
[self.tableView reloadData];
}];
Ignoring syntax problems, does self.tableView cause a retain cycle? Is it necessary to do the following instead?
__weak id weakSelf = self;
[AFnetworking requestGet:@"http://www.website.com" completionBlock:(^)(RequestObj* request, NSError* error){
[weakSelf.tableView reloadData];
}];
Or is there some sort of memory magic that AFNetworking does to prevent this?
Edit courtesy of Aaron Brager
Here you don't have a retain cycle. But if you did, in the completion block, you should convert weakSelf back into a strong reference so it won't get deallocated halfway through your completion block.
id strongSelf = weakSelf;
[strongSelf.tableView reloadData];
weakSelf
back into a strong reference so it won't get deallocated halfway through your completion block. (id strongSelf = weakSelf; [strongSelf.tableView reloadData];
). – Aaron Brager