Reputation: 841
I am trying to add objects to a Core Data Database using the following code:
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
managedObjectContext.persistentStoreCoordinator = [[CoreDataController sharedCoreDataController] persistentStoreCoordinator];
Feed *feed = [NSEntityDescription insertNewObjectForEntityForName:@"Feed" inManagedObjectContext:managedObjectContext];
feed.feedID = dictionary[@"feed_id"];
feed.siteURL = dictionary[@"site_url"];
feed.title = dictionary[@"title"];
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"Error, couldn't save: %@", [error localizedDescription]);
}
Feed is a subclass of NSManagedObject. The persistentStoreCoordinator returned from the sharedCoreDataController (a singleton) is the persistentStoreCoordinator from a UIManagedDocument (created or opened when the app launches). As far as I can tell, the document is being created or opened successfully. I am running this code in the simulator, and I'm looking in the directory in which I am saving the Database (the apps Documents directory), but the persistentStore file is not being updated to reflect the new objects being added. Am I doing something wrong? I should also point out that the above code is being executed multiple times on a concurrent, asynchronous queue.
Any help would be much appreciated, thanks in advance.
Update: After the suggestions from Alexander and Duncan, the code above has been updated to reflect the changes. Sadly, however, I haven't noticed any difference (the new data is not appearing in the persistentStore file).
Upvotes: 0
Views: 217
Reputation: 46718
First, using UIManagedDocument
is not recommended. It is not intended to be your single app wide context. It is meant for document style applications.
Second, the NSManagedObjectContext
that is exposed from the UIManagedDocument
doesn't have a NSPersistentStoreCoordinator
attached to it. It is a child context of a private NSManagedObjectContext
that then has a NSPersistentStoreCoordinator
. I suspect that if you used the debugger you may find that your NSManagedObjectContext
is missing its NSPersistentStoreCoordinator
.
In any event, you are using UIManagedDocument
and then trying to attach another NSManagedObjectContext
to the same NSPersistentStoreCoordinator
. That is a bad design and you should at a minimum remove the UIManagedDocument
or stop creating a new NSManagedObjectContext
.
What is the point of the new context? What are you trying to solve with this code?
Upvotes: 0
Reputation: 8988
Try using something like this
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] init];
managedObjectContext.persistentStoreCoordinator = [[CoreDataController sharedCoreDataController] persistentStoreCoordinator];
for (NSDictionary *dictionary in arrayOfData) {
Feed *feed = [NSEntityDescription insertNewObjectForEntityForName:@"Feed" inManagedObjectContext:managedObjectContext];
feed.feedID = dictionary[@"feed_id"];
feed.siteURL = dictionary[@"site_url"];
feed.title = dictionary[@"title"];
}
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"Error, couldn't save: %@", [error localizedDescription]);
}
I would avoid creating a managedObjectContext for each object you insert.
Upvotes: 0
Reputation:
Have you called the line to save your managedObjectContext
? How about this:
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"Error, couldn't save: %@", [error localizedDescription]);
}
Upvotes: 1