Reload application data when the app comes into foreground?
Asked Answered
R

4

6

I'm new to iPhone development. I'm building an app that loads data from a local sqlite3 DB in

-   (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   

When I tap the iPhone button and put it in the background and then I recall it I see (as normal) the app in the same way I left it. What I would like to do is, when it comes in the foreground, to reload data as if it was called from scratch.

Which is the correct way to do it?
Thanks in advance.

Reft answered 15/10, 2010 at 14:31 Comment(0)
B
13
- (void)applicationDidBecomeActive:(UIApplication *)application {
}

Reload the data in the above function of the app delegate to refresh the data when ever the app comes to foreground.

Bathtub answered 15/10, 2010 at 14:36 Comment(3)
i see a delay of one cycle on actually getting refreshed system prefs (standardUserDefaults) here. eg, 1. app-to-background 2. change app preference 3. app-to-foreground: No Change! 4. app-to-background 5. app-to-foreground: There it is!Nankeen
crazy, so i think the defaults are not being saved by the system preferences until after applicationDidBecomeActive() is called. i determined this because if i just print the value every second or so in my app, it's up to date.Nankeen
you could force the sync of user defaults with [[NSUserDefaults standardUserDefaults] synchronize];Halfcaste
H
63

So in the App Delegate class - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will only be called at the first time you enter the app.Then it will call - (void)applicationDidBecomeActive:(UIApplication *)application.

If your iphone iOS is 4.0 or later, when a user click on the home button, it will first invoke - (void)applicationWillResignActive:(UIApplication *)application, then - (void)applicationDidEnterBackground:(UIApplication *)application.

Then the app will run in background till the user kill it. When the user enter the app again, it will first invoke - (void)applicationWillEnterForeground:(UIApplication *)application, then - (void)applicationDidBecomeActive:(UIApplication *)application.

So related to your question, you should call either applicationWillEnterForeground: or applicationDidBecomeActive: to reload your data. Though in the xcode's comment of these methods, Apple suggests use applicationDidBecomeActive: to restart paused tasks and/or update user interface; while in the applicationWillEnterForeground:, you can undo the changes you've made in entering the background.


So to make it easier to see, i put a number tag to each method. Here's when the method to be called.

0 application:(UIApplication *)application didFinishLaunchingWithOptions: 
1 applicationDidBecomeActive: 
2 applicationWillResignActive: 
3 applicationDidEnterBackground: 
4 applicationWillEnterForeground: 
  • First enter the app: call 0, then 1;

  • Hit home button: call 2, then 3;

  • Double hit home button(multitasking): call 2;

    • if user choose another app or hit home button again: call 3;

    • if user double hit home button again: call 1;

  • Enter app again: call 4, then 1;

Heir answered 15/10, 2010 at 16:31 Comment(0)
B
13
- (void)applicationDidBecomeActive:(UIApplication *)application {
}

Reload the data in the above function of the app delegate to refresh the data when ever the app comes to foreground.

Bathtub answered 15/10, 2010 at 14:36 Comment(3)
i see a delay of one cycle on actually getting refreshed system prefs (standardUserDefaults) here. eg, 1. app-to-background 2. change app preference 3. app-to-foreground: No Change! 4. app-to-background 5. app-to-foreground: There it is!Nankeen
crazy, so i think the defaults are not being saved by the system preferences until after applicationDidBecomeActive() is called. i determined this because if i just print the value every second or so in my app, it's up to date.Nankeen
you could force the sync of user defaults with [[NSUserDefaults standardUserDefaults] synchronize];Halfcaste
P
4

You can exit() your application when iOS notifies it that it should run in the background like so:

- (void)applicationDidEnterBackground:(UIApplication *)application {
  exit(0);
}
Pep answered 15/10, 2010 at 14:36 Comment(1)
I wouldn’t do this by hand – there’s a UIApplicationExitsOnSuspend Info.plist key for applications that do not wish to run in background.Caseation
H
3

I think that is the delegate of UIApplication:

- (void)applicationWillEnterForeground:(UIApplication *)application

In iOS 4.0 and later, this method is called as part of the transition from the background to the active state. You can use this method to undo many of the changes you made to your application upon entering the background. The call to this method is invariably followed by a call to the applicationDidBecomeActive: method, which then moves the application from the inactive to the active state.

Happiness answered 15/10, 2010 at 14:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.