Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

refresh data after entering a foreground

Tags:

ios

iphone

After changing the default settings, I would like to refresh data of myViewController when I enter the foreground in AppDelegate. What I do is

AppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [window addSubview:[navigationController view]];
    NSLog(@"APPLICATION DID FINISH LAUNCHING");
    // listen for changes to our preferences when the Settings app does so,
    // when we are resumed from the backround, this will give us a chance to update our UI
    //
    [[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(defaultsChanged:)
                                           name:NSUserDefaultsDidChangeNotification
                                         object:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     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.
     */
    NSLog(@"APPLICATION WILL ENTER BACKGROUND");
    [myViewController viewDidLoad];
}

myViewController.m

- (void)viewDidLoad
{
    NSLog(@"VIEW DID LOAD IN MY VIEW CONTROLLER");
    // watch when the app has finished launching so we can update our preference settings and apply them to the UI
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSettings:) 
                                        name:UIApplicationDidFinishLaunchingNotification object:nil];
}

- (void)updateSettings:(NSNotification *)notif
{
    myLabel.text = @"data has just been modified";
}

However, there is nothing changed at all.

like image 310
tonytran Avatar asked Jan 27 '12 20:01

tonytran


1 Answers

You have 2 problems in your code. First the sequence of the start-up functions (what is called Execution States) seems not clear to you, and second you added a listener to the function updateSettings but you never called it in the code above, and this is why nothing changed when your app starts.

Let me first explain the start-up sequence. When an app loads from a turned off device these states are fired:

application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:

After that if you press the Home button these states are fired:

applicationWillResignActive:
applicationDidEnterBackground:

Then if you enter into the app again, the following will occur:

applicationWillEnterForeground:
applicationDidBecomeActive:

Notice that the loading state only occurs once at the first load (but not after you return from a Home press). Now for every view the function viewDidLoad will be called only one time which is the first time this view was called. If call this view again (after it has been loaded) then the function viewWillAppear will be called instead. So usually refreshing occurs in viewWillAppear function.

An important thing that I noticed in your code which is improper is the usage of the main delegate functions. In applicationWillEnterForeground you manually called viewDidLoad while you shouldn't do that as this function will be called automatically as I explained above. Also I see you are adding Notification centers that are not needed.

Now lets see the second problem in your code. You are adding a Notification Center for the function updateSettings in the viewDidLoad. Well the loading of this view will occur after the event UIApplicationDidFinishLaunchingNotification therefore in practice you never called the function updateSettings. Moreover since this function is a member of your class you don't need a Notification Center to call it. We usually use a Notification Center when we need to call a function from another class. Simply what you need to do is call this function directly from viewDidLoad as follows:

[self updateSettings]

And if you need to update after the home button is pressed call the function from viewWillAppear.

I hope this fast explanation helps you.

EDIT: to answer your comment below

If you have only one view (no navigation controllers...), after it appears the first time it will stay in the memory and it won't appear again (so this function isn't called). Here you should catch the event UIApplicationDidBecomeActiveNotification so do the following:

In the viewDidLoad add a Notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSettings) name:UIApplicationDidBecomeActiveNotification object:[UIApplication sharedApplication]];

This will allow the function updateSettings to be called every time your app wakes. Also remember to remove this listener at the end by:

[[NSNotificationCenter defaultCenter] removeObserver:self];
like image 61
antf Avatar answered Nov 06 '22 00:11

antf