better completion block handling

make sure to remove the completion block once done.
This should help in cases the user forgot to do the weak-dance
as well as when the copied objects are release
This commit is contained in:
Stephan Diederich 2013-08-15 22:31:53 +02:00
parent ff555cb87d
commit c39053dbd2

View File

@ -93,15 +93,16 @@
} }
- (void)setCompletion:(BITNetworkCompletionBlock)completion { - (void)setCompletion:(BITNetworkCompletionBlock)completion {
__weak typeof(self) weakSelf = self; if(!completion) {
if(nil == completion) {
[super setCompletionBlock:nil]; [super setCompletionBlock:nil];
} else { } else {
__weak typeof(self) weakSelf = self;
[super setCompletionBlock:^{ [super setCompletionBlock:^{
typeof(self) strongSelf = weakSelf; typeof(self) strongSelf = weakSelf;
if(strongSelf) { if(strongSelf) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
completion(strongSelf, strongSelf->_data, strongSelf->_error); completion(strongSelf, strongSelf->_data, strongSelf->_error);
[strongSelf setCompletionBlock:nil];
}); });
} }
}]; }];