我正在尝试解决以下问题: - 如果用户强制退出应用程序然后在强制退出后进入应用程序,我想从Core Data中获取一些数据,然后在视图控制器上显示它。
我试过使用以下观察者:
notification.addObserver(self, selector: #selector(reloadTimerOnAppStart),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)
每次加载应用程序时,此观察者都会工作,但应用程序第一次加载时除外。
e.g。 - 强制退出应用程序 - 运行应用程序(没有任何反应) - 关闭应用程序 - 打开应用程序(选择器工作)
它第一次无法正常工作。
我是否在错误的地方实例化了这个?它应该发生在appDelegate:
中func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// check if db has any outstanding running tasks
// if it does then start the singleton timer
// display it accordingly on the view.
}
应该调用applicationDidBecomeActive,所以我只是不确定发生了什么。
在另一个注释中,如果你将UIAlert放在appDelegate.swift文件的这个函数中
func applicationDidBecomeActive(_ application: UIApplication) {
AlertUser()
// with UIAlertController //
}
然后每次出现此警报!即使是在应用程序强制退出后的第一次加载,希望能帮助我解决这个问题,以便我能够实际为视图控制器工作。
答案 0 :(得分:1)
我认为该方法无法调用,因为您正在强制关闭应用,这会打破应用的生命周期,这可能对您没有多大帮助,但想一想。