工作生活

NSNotificationCenter postNotific

2019-07-02  本文已影响0人  丢了理想

postNotificationName:XXXXXX carsh

[[NSNotificationCenter defaultCenter] postNotificationName:XXXXXX object:self];

经过推理得出结论:
因为通知是全局异步的,self有可能被释放掉了,或许通知没用对数据加锁or something
网上搜索也得出了相似的答案

这样使用为什么会有崩溃问题呢?
因为会随机遇见僵尸内存问题

Are there any workaround for randomly crash?

Enable zombies - this will cause the exception to breakpoint on the offending line. From there it will be a lot easier to figure out than the general EXC_BAD_ACCESS crash.

那如何解决呢?
1 .翻看sdwebimage源码看到了如下代码(本人未验证)(以测试,没有用)

__block typeof(self) strongSelf = self;

[[NSNotificationCenter defaultCenter] postNotificationName:XXXXXX object:strongSelf ];

2.转地址(本人未验证)
地址获取与此类似

__block typeof(self) strongSelf = self;
NSInteger  nSelf = (long)(__bridge void *)self;
NSNumber  *number = @(nSelf);
[[NSNotificationCenter defaultCenter] postNotificationName:XXXXXX object:number];

//接受通知对象转换成self
NSObject *obj = (__bridge NSObject*) object.integerValue;

上一篇 下一篇

猜你喜欢

热点阅读