Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于第 34 题关于 NSOperation 中需要手动添加 Autorelease Pool 的部分的疑问 #25

Open
skyline75489 opened this issue Nov 13, 2015 · 4 comments

Comments

@skyline75489
Copy link

SO 上的这个答案 https://stackoverflow.com/questions/24562043/is-autoreleasepool-still-required-for-modern-ios-8-nsoperation-usage 提到,系统会自动设置好 Autorelease Pool。

这个答案 https://stackoverflow.com/questions/184409/nsautoreleasepool-in-nsoperation-main 提到说需要手动加入 Autorelease Pool,但是答案本身是 08 年的,所引用的苹果官方文档应该也已经更新了(搜不到对应的内容)。

按照我自己的理解,应该是不需要的,因为 NSOperation 所跑的线程也是 Cocoa 创建的线程,Cocoa 创建的线程中应该就已经设置好 Autorelease Pool 了。

按照文档的说法,Operation 里的 Pool 不一定什么时候会进行 drain,所以添加自己的 Pool 会更利于管理。

@ChenYilong
Copy link
Owner

关于你提到的:

Cocoa 创建的线程中应该就已经设置好 Autorelease Pool 了。

子线程中的 Autorelease Pool 不会工作,需要手动开启。因为子线程的runloop默认不工作。

@skyline75489
Copy link
Author

Pool 的工作依赖于 runloop 这一点我倒是没想到,如何手动开启 pool 呢?就是自己再添加一个吗?

@liuxuan30
Copy link

@ChenYilong 子线程 runloop默认不工作和 autoreleasePool 不会工作这个是哪里的文档?

@michaeldong
Copy link

到底需不要加autoreleasepool?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants