I am trying to solve the following problem: - If a user force quits the app and then enters the app after force quitting, I want to fetch some data from Core Data and then display it on the view controller.
I have tried to use the following observer:
notification.addObserver(self, selector: #selector(reloadTimerOnAppStart),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil)
This observer works every time the app loads with the exception of when the app loads for the very first time.
e.g. - force quit the app - run the app (nothing happens) - close the app - open the app (the selector works)
It just doesn't work the first time around.
Am I instantiating this in the wrong place? should it happen inside the 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.
}
According to the most upvoted answer on here applicationWillEnterForeground vs. applicationDidBecomeActive, applicationWillResignActive vs. applicationDidEnterBackground
applicationDidBecomeActive should be called, so I am just a bit unsure as to what is happening.
In another note, if you put a UIAlert in this function of appDelegate.swift file
func applicationDidBecomeActive(_ application: UIApplication) {
AlertUser()
// with UIAlertController //
}
Then this alert appears every time! even on the first load after the app has force quit, hoping for some help as to how I can get around this so that I can actually get this working for the view controller.