我刚刚花了一天时间追踪一个非常奇怪的错误。这是一个过度发布的 UILabel,尽管在代码中没有过度发布。如果我注释掉标签的发布,代码就可以了。
我将其追踪到正在后台线程中发布的 UIView。这似乎不正确,因为您应该从主线程访问所有 UI 元素。然后我找到了一个在后台线程中加载的 nib 文件:
[[NSBundle mainBundle] loadNibNamed:@"nib name" owner:self options:nil];
我不需要详细说明代码在做什么,但我的问题是:
如果在后台线程中调用 loadNibNamed 并且加载的 View 被 nib 加载过程缓存,那么主线程也想要加载该 nib - 但从缓存中加载它。当我的对象被释放时 - 从背景中的 Nib 加载的 View 是否也在背景中释放..??真的没有意义,因为无论如何后台线程将不再存在....
我不知道,但我通过将 GCD 调度中的 loadNibNamed 部分代码同步包装到主线程来解决它。
最佳答案
来自 Mike Ash 的文章
Dangerous Cocoa Calls
NSBundle This one has half of the problems of NSHost. NSBundle returns shared objects, but is not thread safe, so it's main-thread only. It's still safe to use from the main thread. The reason I mark it as dangerous is because the fact that it's unsafe to use from secondary threads is not really documented, but rather has to be inferred from the fact that it's not thread safe and the fact that the instances are shared, and it can be tempting to use it from other threads.
关于objective-c - 在后台线程上加载 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9264850/