3

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.

soleil
  • 12,133
  • 33
  • 112
  • 183
  • How are you creating the persistent store, coordinator and context? – Wain May 23 '13 at 17:15
  • 1
    Maybe you didn't save changes. It seems unlikely but without some code or at least a good description of how it works, it's the first and most obvious possibility. – Tom Harrington May 23 '13 at 17:43
  • Well, there's too much code to paste here, so I was just wondering if there's a scenario that could cause this. After further testing, it appears to happen in the simulator but not on the device. The device recognizes the stored data and doesn't create the objects from scratch. Any idea why the simulator wouldn't persist? – soleil May 23 '13 at 17:58
  • When you save changes in Core Data and it fails you get an ‘NSError‘ telling you what happened. Did you check this? What did it say? – Tom Harrington May 23 '13 at 18:15
  • Is the sample code working for you? How does your code differ from the sample code? – GoZoner May 23 '13 at 18:37
  • @TomHarrington I am saving changes, but it seems like I have to do both self.objectContext save: and [[CoreDataController sharedController] saveContext]; If I don't do both the objects wont persist. – soleil May 23 '13 at 23:32
  • Yes, fine, but you're still not answering the question, and now you're introducing new classes (`CoreDataController`) without describing them at all. I don't know what's wrong, and you're going out of your way to help people try to help you. – Tom Harrington May 23 '13 at 23:36

2 Answers2

1

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

iDevzilla
  • 15,382
  • 1
  • 16
  • 16
  • The firefox addon you mentioned looks like it might be useful. I have installed it, but how do I point it to my app's core data DB file? – soleil May 23 '13 at 22:12
  • Nevermind on the firefox question. I figured that out. What's the difference between self.objectContext save: and [[CoreDataController sharedController] saveContext]; ? It seems like I have to both? – soleil May 23 '13 at 22:55
0

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.

LavaSlider
  • 2,494
  • 18
  • 29