Question

I'm running into a strange problem with Core Data. In a particular view controller I'm loading the views from objects in Core Data. When I run the app, the first time it loads this view Core Data returns nothing from my fetch. So I repopulate Core Data, and every time the view is displayed after that it correctly fetches the objects from Core Data. However, each time the app is launched, it doesn't find anything in Core Data and then has to create the objects from scratch again.

So what would cause Core Data objects to persist while the app is running but not between launches? I'm not doing anything to delete any objects.

EDIT: And is there any way to view what is actually in Core Data? Like a file or something I could look at? That would make it easier to debug this.

No correct solution

OTHER TIPS

Make sure that you are saving the context after your changes. The template method is:

- (void)saveContext {
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil) {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NKLOG(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

If you want to open your database, you can try this firefox addon called SQLite Manager

And search for your .sqlite file, the default path for your app would be:

/Users/YOUR_USER/Library/Application Support/iPhone Simulator/IOS_VERSION/Applications/GENERATED_HASH/Documents/YOUR_APP.sqlite

All the files for you application can be inspected by finding where the simulator has put your application. You can put this out with an NSLog( @"My database is at: '%@'", theDatabaseURL.path );

Since everything you do in an NSManagedObjectContext is maintained in memory it makes sense that it would persist while the application is running but be gone the next time the application launches if the persistent store is not being set up properly or a save operation is not being triggered.

It might help if you showed the part of your code where you open, initialize, and save your data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top