GithubHelp home page GithubHelp logo

Comments (2)

oa414 avatar oa414 commented on August 16, 2024

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.

RhettTamp avatar RhettTamp commented on August 16, 2024

嗯,我大概也是你这个意思,生成那个强引用主要就是防治self提前释放吧,但是它并不会造成引用循环,因为代码块结束后它还是要自动释放的

from objc-zen-book-cn.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.