0

I have a Core Data model which works well importing and exporting data. The way my app works is that it downloads a JSON file from the database, parses it, flushes out the core data model, then adds the data again (effectively a refresh of the local data model).

If I make a change to the database, the change is reflected in the JSON file, but is not reflected in the core data model until I restart (aka end the app, open again) the app.

Im sure it must be something to do with the way I am flushing out the database, but I just can't put my finger on it. I've included some code below to help.

The method I'm using to flush out the data model:

- (void)resetCoreData;
{    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"AppWithCoreData.sqlite"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    [fileManager removeItemAtURL:storeURL error:NULL];

    NSError* error = nil;

    if([fileManager fileExistsAtPath:[NSString stringWithContentsOfURL:storeURL encoding:NSASCIIStringEncoding error:&error]])
    {
        [fileManager removeItemAtURL:storeURL error:nil];
    }

    persistentStoreCoordinator = nil;
    managedObjectContext = nil;

    [self managedObjectContext]; // Rebuild Object Context    
}

One line of the code I'm using to add the data to the data model:

[model setValue:[dictionary objectForKey:@"eventID"] forKey:@"eventID"];
Alex
  • 3,031
  • 6
  • 34
  • 56
  • Do you refetch data afterwards? Also look at this: http://stackoverflow.com/a/5770216/730701. The only difference is that you remove the file before setting context to nil and that guy does it after. – Adam Jul 22 '12 at 15:52
  • Yes, I call this method, then re-fetch the data. It saves without any errors. When I try to save after moving those two lines after setting the context, I get error Couldn't save: The operation couldn’t be completed. (Cocoa error 134030.) – Alex Jul 22 '12 at 16:55

1 Answers1

2

Usually when you need to save your changes to your core data store you need to save them.

To achieve it you need to call

NSError* error = nil;
[managedObjectContext save:&error];

In this manner changes you have performed are saved to disk. In fact when you simply change, for example, the value of an attribute on a specific entity, it's only available in memory.

I suppose the changes are available only at the next restart since in your app delegate you listen for applicationWillTerminate delegate and there you save the context. Are you using the Core Data Stack provided with Xcode template?

If possible provide more details, maybe I could help you.

Hope that helps.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Sorry, I am calling save, I just foolishly left that out. I am not using the core data template, I added Core Data afterwards. Everything else about core data works, but this. Thanks for your help so far. – Alex Jul 22 '12 at 16:53
  • @AlexGodbehere Could you provide some other details? – Lorenzo B Jul 22 '12 at 16:55
  • Okay, I have an update. I have downloaded the data in the model after the app has launched but BEFORE I usually delete the data and add the new. The result... Empty. So the data must not be being saved in the first place. I find this strange as it does not report an error. What other information would you like? – Alex Jul 22 '12 at 17:16
  • @AlexGodbehere Sorry for the delay. Could you extend your question to add what you tried? What do you mean with *the data must not be being saved in the first place*? Thanks. – Lorenzo B Jul 24 '12 at 07:29
  • Sure! Basically, I have a log in screen in my app, which when used by the user to log in, presents the main View Controller. In the didLoad method of this VC I am downloading the data from the server, parsing the JSON file, 'flushing' out the data model and importing the new downloaded data. I have added a debug 'Print Data model to console' to this view controller. When pressed after the initial download, it shows the correct data which reflects the servers data. For example, I will now delete one record on the server. – Alex Jul 24 '12 at 20:10
  • When I press 'refresh', the same process executes (download, parse, flush, save new data), and the console shows the new json file without the record is being downloaded. However, when I press the print button, it still shows the old data in the data store. I then had an idea to put a print button on the login screen before the initial download to see if the data from the previous session was there. This print button shows the persistent store being empty. I'm baffled. If you have any idea, I'd be extremely grateful. – Alex Jul 24 '12 at 20:13