Comments (2)
hi, 我调整了一下 issue 中代码的排版。
这个问题原文也没有用很确定的语气,我也是来共同探讨下。
如果直接在 block 内使用 block 外定义的 weakController, 那么在多线程执行的时候,比如在 block 内
if (weakController) {
doA();
doB();
}
如果在 doA() 完成后,其他线程获取了控制权,减少了 weakController 的 retain,那么 weakController 可能就会变成 nil,继续执行代码块的 doB() 时候就出问题。
如果是以上述苹果文档里的方式执行:
MyViewController *strongMyController = weakMyController;
if (strongMyController) {
doA();
doB();
}
strongMyController 是在代码块运行的时候生成强引用,并且保证在这个 block 运行的时候,对象不被释放。所以原文说道
在 block 内用强引用的优点是,抢占执行的时候的鲁棒性。
...
和并发执行有关。当涉及异步的服务的时候,block 可以在之后被执行,并且不会发生关于 self 是否存在的问题。
这样就可以解释通了。
不过这个问题 苹果的文档 和其他讨论 也没说得很明白,要彻底弄清楚恐怕还是需要查阅更多资料和做实验研究下。我暂时没想到如何精确找到 strong reference of weak reference in blocks
相关资料的方法。欢迎进一步探讨。
from objc-zen-book-cn.
嗯,我大概也是你这个意思,生成那个强引用主要就是防治self提前释放吧,但是它并不会造成引用循环,因为代码块结束后它还是要自动释放的
from objc-zen-book-cn.
Related Issues (15)
- 第六章没有翻译完 HOT 2
- Gitbook链接 HOT 1
- 是不是太着急了 HOT 1
- 翻译错误 HOT 11
- 关于 Secondary initializer 段落的翻译问题 HOT 15
- practice HOT 2
- GitBook中文版的封面设计 HOT 1
- 常量extern的写法在Xcode7中编译不通过 HOT 1
- 翻译错误 HOT 1
- block循环应用那里有个错误??
- 一个小错字 HOT 1
- 关于"重载"和"重写" HOT 2
- 你在第一次访问的时候改变了初始化的消耗,产生了副作用,这会让优化性能变得困难(以及测试) HOT 1
- It's interesting to understand why do we need to set the value of `self` with the return value of `[super init]` and what happens if we don't do that. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objc-zen-book-cn.