- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正试图正确地避免在 Objective C 中使用 block 的保留循环,并且不确定是否具有嵌套 block 。
如果我像这样写一个简单的 block :
[self doSomethingWithBlock:^{
[self doSomethingElse];
}];
编译器捕获并警告我这可能会导致保留循环。我将其更改如下以避免循环:
__weak __typeof(self)weakSelf = self;
[self doSomethingWithBlock:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf doSomethingElse];
}];
当我写这样的东西时:
[self doSomethingWithBlock:^(MyObject* object){
[object doSomethingElseWithBlock:^{
[self doYetAnotherThing];
}];
}];
编译器很高兴,但我不相信它是安全的。即使中间有一个 object
,它在概念上看起来仍然和上面一样,但现在它是一个有 3 个 retains 的循环。
应该是这样吗?
[self doSomethingWithBlock:^(MyObject* object){
__weak __typeof(self)weakSelf = self;
[object doSomethingElseWithBlock:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf doYetAnotherThing];
}];
}];
还是这样?
__weak __typeof(self)weakSelf = self;
[self doSomethingWithBlock:^(MyObject* object){
[object doSomethingElseWithBlock:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf doYetAnotherThing];
}];
}];
最佳答案
在这种情况下,您不必担心循环引用。您担心的是实际上不再需要对象 self 的情况,但是在嵌套 block 中使用 self 会使它不必要地保持事件状态。例如,如果你有一个 View Controller ,当 View 被屏幕移除时应该消失,但你下载了一张你想在 Controller View 中显示的图像。如果图像在 View 消失后很长时间才到达,那么您不希望 View Controller 再事件。
最好的是
__weak typeof (self) weakSelf = self;
在调用最外层方法之前。然后在每个应该使用 self 的 block 中,添加
typeof (self) strongSelf = weakSelf;
并在该 block 中使用 strongSelf。根据情况,您可能想检查 strongSelf 是否在此时不为 nil,但是当它为 nil 时向 strongSelf 发送消息无效,因此如果您所做的只是发送消息并获取或设置属性,那么检查因为没有必要。
如果不这样做会怎样?不同之处在于,如果您在任何地方(或仅在最里面的 block 中)都使用 self ,那么 self 可能会不必要地保持在最里面的 block 中。
关于ios - 我应该在嵌套 block 中使用 weakSelf 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38835792/
这个问题在这里已经有了答案: Does calling a method inside a block that calls another method referring to self cau
我看到了两种不同的实现... __weak UIDataType *weakSelf = self; 和 UIDataType __weak *weakSelf = self; 但它们似乎都有效。引擎
Closed. This question is opinion-based。它当前不接受答案。 想要改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。 4年前关闭。
在以下代码片段 (self.searchResults) 中调用 self 时,我是否应该使用 weakSelf: [self.restaurants enumerateObjectsUsingBlo
有一个关于 weak self 、 blocks 和 retain cycle 的问题。 根据这本书,我明白我们需要在 block 中使用 weakself..问题是,什么时候? 例如,简单的动画代码
我正在查看他们 2014 年 WWDC session “带有 Collection View 的高级用户界面”的一些 Apple 示例代码,并发现了如下所示的 weakself 声明: __weak
weakSelf 的几乎每个例子我在网上遇到的模式有语法 __weak typeof(self) weakSelf = self; 但是编译器在最新版本的 Xcode 中提示这种语法,要求: __we
所以不知何故,我的 weakSelf 变量在我的 block 有机会执行之前被释放了。这只发生在一个特定的场景中,其他时候我点击这个 block 它工作正常。这是我的代码的样子: __weak typ
如果我这样做可以吗: -(void)example{ __weak __typeof__(self) weakSelf = self; dispatch_queue_t
阅读 my own answer .我完全理解为什么我们需要成员/属性的 weakSelf。他们可以创造内存周期。但是属性有一个内存位置。函数也有内存位置吗?!我的意思是不是一个功能只是在旅途中发生的
好的,所以我可能还没有完全掌握何时应该使用 weakSelf以 block 为单位。我知道这样做是为了防止保留周期等等,但我听说这条规则有一些异常(exception)。 在以下代码中,我检查 API
我有以下代码: [[AHPinterestAPIClient sharedClient] getPath:requestURLPath parameters:nil suc
我的代码中有很多 block 。我有一个在登录时初始化用户的过程,我使用 Parse.com 作为我的后端: PFQuery *messageBankQuery = [PFQuery queryWit
我正试图正确地避免在 Objective C 中使用 block 的保留循环,并且不确定是否具有嵌套 block 。 如果我像这样写一个简单的 block : [self doSomethingWit
我知道当我们使用 block 时我们应该使用 weakSelf 来避免 retain cycle。但是我看到有时 block 中有一个 strongSelf。 让我困惑的是: 为什么一定要把weakS
我理解为什么我们会在一个 block 中使用 weakSelf,只是不那么多。 我正在将一个代码库转换为 ARC,它给出了很多带有 block 的保留周期警告。从我收集到的文档中,我需要更改此内容:
这个问题在这里已经有了答案: Strong reference to a weak references inside blocks (2 个答案) 关闭 6 年前。
我听说我应该始终在 block 中使用 weakSelf 以避免循环保留,但是调度 block 呢?在这种情况下,我的方法在以下代码中处理来 self 的服务器的错误响应: //handling se
我已经为这个问题苦苦挣扎了一段时间,因为我不认为我完全理解保留周期。我对此完全陌生,我正在尝试了解更多相关信息。 我收到带有以下代码的 EXC_BAD_ACCESS 消息。 我开始使用 weakSel
例如,我正在使用 SVInfiniteScrolling ( https://github.com/alexanderedge/SVInfiniteScrolling )。 我有一些看起来像这样的代码
我是一名优秀的程序员,十分优秀!