简体   繁体   中英

when should i store and re-store to keychain on ios swift?

I see in the appDelegate few methods and I'm not sure if storing and re-storing user state in just some of them covers all scenarios?

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

    stopTasks()
    setSharedPrefrences()
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    stopTasks()
    setSharedPrefrences()
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.

    startTasks()
    getSharedPrefrences()
}

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.
    startTasks()
    getSharedPrefrences()
    connectGcmService(application)

}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    stopTasks()
    setSharedPrefrences()
    disconnectGcmService(application)
}

should I store\\restore in only some of them? when should I disconnect and reconnect to GCM service?

are my restore redundant?

Keeping a local flag saying is restore has been made is not practical, as the application might destroy it?

Read about The App Lifecycle: Execution States for Apps in Apple's App Programming Guide for iOS.

Also, the documentation in UIApplicationDelegate for the methods you mention in your question contains very detailed information when they are called. Take the documentation for applicationWillResignActive as an example.

  • didEnterBackground is always preceded by willResignActive , so there is no need to run the same code.

  • willEnterForeground is always followed by didBecomeActive , but didBecomeActive can also be called in other situations (see below).

  • willResignActive can be called without didEnterBackground being called, eg a 10% battery warning or a phone call comes. If the user declines the call, your app will remain in the foreground and didBecomeActive is called next to tell you that the app is active again.

  • willTerminate is never called in modern iOS apps. It was used in iOS 2 and 3 before iOS supported multitasking. Since apps nowadays move to the background when the user "quits" them, this event is no longer used. (When the OS kills your app due to memory pressure while it is in the background, it gets killed right away without executing any more code.)

In summary:

  • stopTasks / startTasks should be in willResignActive and didBecomeActive .
  • Saving/restoring user data can be done in willResignActive / didBecomeActive or didEnterBackground / willEnterForeground . I would probably choose the latter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM