Pergunta

Conditions

  • Getting a notification (txt message)
  • or Opening/Closing Notification Center
  • or Switch to another app, come back

Basically, the app comes back from background for a short period.

Issue

Layout is ok, but some of my properties that I get from CoreData are empty after resuming from Background. This issue is there on pretty much all of my view controllers.

The project

This is an app that has a main tabbar controller, with two navigationcontrollers within the tabs and maybe two levels of viewcontrollers, that have themselves child UIViews (that use some of the informations). The back-end is made of Parse and CoreData.

The weird part

Back from background -> properties are ok on viewWillAppear (Create a backup of the id) -> they are nil seconds after -> I need to manually get them back (from the ID I just stored)

Here is a screenshot, when putting a break point within a function called every 5sec to check the current time (link to bigger) :enter image description here

What I did

NSCoder implemented for state restoration and every view controllers have a restoration ID, however it doesn't get called when app is becoming active. I don't think NSCoder is the issue here since from the documentation it is used when the OS will kill it on its own, or a force quit from the user.

I tried to manually refresh the content in the appropriate ViewControllers from NSNotificationCenter if the NSManagedObjects are nil, but it is not future-proof, and is never the same logic on each view.

Any thoughts? Thank you!

My AppDelegate :

- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"Will Resign Active");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Entered Background");  
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"Will enter Foreground");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"needsRefresh" object:self userInfo:nil]; // Helpful in some viewcontrollers to save variables that are not nil.

    [PF_FBSession.activeSession handleDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"Will Terminate");
}
Foi útil?

Solução

For those finding this thread, here is the solution I found.

My issue and answer was specific to my project.

I had another controller, the one that takes care of all my database connections, that was listening to applicationDidBecomeActive. This was calling up a refresh of my data, but also a "cleanup", that was deleting/editing some NSManagedObjects, then saving.

Conclusion : the memory address wasn't the same, the object wasn't considered the same, and therefore was empty on my current page.

I fixed it by stopping cleaning up on each AppDidBecomeActive, but move that logic to AppDidFinishLaunching instead.

Hope that helped!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top