- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前在我的 Objective-C 应用程序中使用此方法:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleEnteredForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
如果我不使用
删除它[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
我有内存泄漏的风险吗?或者有没有一种好方法可以利用此通知而不会有内存泄漏的风险?
最佳答案
没有内存泄漏的危险;通知中心对您的观察者 self
的引用很弱。
但是存在一个危险——即self
将不复存在并且通知中心稍后将尝试向它发送通知。这将导致可怕的崩溃,一个很难追踪的崩溃(悬挂指针)。
这就是为什么您必须确保在 iOS 8 及更早版本中注销您的观察者。
然而,从 iOS 9 开始,这不再是问题,因为通知中心对您的观察者的引用不是简单弱的,而是 ARC 弱的。这意味着对已释放观察者的引用变为 nil
。通知中心检测到这一点并停止向其安全地发送通知。
关于没有内存泄漏的 iOS UIApplicationWillEnterForegroundNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48526232/
我在 View Controller 中使用此函数来识别应用程序是否再次处于事件状态并刷新一些数据: [[NSNotificationCenter defaultCenter]
我目前在我的 Objective-C 应用程序中使用此方法: [[NSNotificationCenter defaultCenter] addObserver:self selector:@sele
这是一个很简单的问题。在我的 View Controller 中,我正在注册“前台”通知: override func viewDidLoad() { super.viewDidLoad()
我有一个问题,当应用程序从后台返回时,UIView 的 ViewWillAppear 方法不会触发。这是一个问题,因为我的主应用程序屏幕显示从用户设置检索的值,如果用户在应用程序处于后台时更改了这些值
我了解到我的应用程序在多任务 iOS 上表现不佳,以防退出应用程序时某个 ViewController 处于事件状态。该 View Controller 需要收集最近的信息才能显示它。到目前为止,用于
我正在编写一个使用 GCD 定时器源的 Objective-C 包装器。目标之一是让包装器在应用程序进入后台时管理暂停计时器源。 docs对于 -[UIApplicationDelegate appl
我是一名优秀的程序员,十分优秀!