问题描述
在我的类中,我创建了此方法。
In my class i have created this method.
-(void) refreshDatasourceWithSuccess:(CreateDataSourceSuccessBlock) successBlock
failure:(CreateDataSourceFailureBlock) failureBlock;
然后我这样调用:
[self refreshDatasourceWithSuccess:^(NSArray* array){
//Success block
[self setDataSource:array];
[self.tableView reloadData];
} failure:^(NSError* error){
// failure block
[self showConnnectionError];
}];
这是一个保留周期,因为我引用 self
在完成块中?
(我没有收到任何警告)
Is this a retain cycle because I reference self
inside the completion block? (I do not get any warning)
更新:
UPDATE:
IN另一个类在这种情况下,我得到一个警告为保留周期
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];
}];
推荐答案
假设你的类中没有变量存储该块,则这两个示例都不是保留周期。该块具有对self的引用,但self不保留对块的引用。
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.
在第二种情况下,由于方法的命名,您会收到警告。它以set开头,因此代码分析器假定它正在设置类的变量。给它一个不同的名称,警告应该消失。
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.
这篇关于保留完成块中的周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!