3

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.

Sohil Pandya
  • 755
  • 1
  • 7
  • 19
  • where exactly is the code for adding the observer? maybe you add the observer after the notification was already sent – Catalina T. Jun 01 '17 at 17:29
  • @CatalinaT. I'm adding the observer inside of viewDidLoad() is that the right place to call it? – Sohil Pandya Jun 01 '17 at 20:22
  • I believe that is the problem, because your VC does not exist when the notification is being sent for the first time, so the observer is added after the notification was already sent. This is the reason why it works for all subsequent calls when the app comes from the background but not the first time. How about you put the code you want executed in AppDelegate in applicaionDidBecomeActive? would that not work? – Catalina T. Jun 02 '17 at 11:29

1 Answers1

2

I think the method can't be called because you are forcing the app closing and this breaks the life cycle of the app, maybe this not help you a lot but think for a second.

Angel Ruiz
  • 31
  • 6
  • I am not so sure, because you can still see the alert in the applicationDidBecomeActive method inside of appDelegate.swift – Sohil Pandya Jun 01 '17 at 17:11