In my class i have created this method.
-(void) refreshDatasourceWithSuccess:(CreateDataSourceSuccessBlock) successBlock
failure:(CreateDataSourceFailureBlock) failureBlock;
Then I call it like this:
[self refreshDatasourceWithSuccess:^(NSArray* array){
//Success block
[self setDataSource:array];
[self.tableView reloadData];
} failure:^(NSError* error){
// failure block
[self showConnnectionError];
}];
Is this a retain cycle because I reference self inside the completion block?
(I do not get any warning)
UPDATE:
IN Another class in this case I get a warning for retain cycles
typedef void (^SetFavoriteCompletionBlock)(NSError*);
-(void)setFavoriteFriend:(BOOL)pSetFavorite
completion:(SetFavoriteCompletionBlock)completionBlock
{
//....
completionBlock(error);
}
And then in this call I get the warning
[self setFavoriteFriend:setFavorite
completion:^(NSError *error){
[self.tableView reloadData];
}];
Assuming you do not have a variable in your class that stores the block, then neither examples are retain cycles. The block has a reference to self, but self does not keep a reference to the block.
You get the warning in the second case because of the naming of the method. It starts with “set”, and therefore the code analyzer assumes it is setting a variable of your class. Give it a different name, and the warning should go away.
However, it is a strange way of coding with the completion blocks if you only call them synchronously from the method. Therefore, I suspect you actually do store the blocks somewhere, and then call them asynchronously. In that case it might be a retain cycle, depending on how and where you store them.