How not to save NSManagedObjects retrieved from NSManagedObjectContext
I am parsing an xml file containing books, for each new node I go:
Book *book = (Book*)[NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:managedObjectContext];
To get the NSManagedObject for my main database, I then go on to populate the book managed object with data, add it to an array, flush it, iterate.
When I'm done, I present a list of books to the user. I haven't done the save yet:
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"%@", [error domain]);
}
Now the user selects one of the books that I would like to keep, but only this one, all the other books are no longer of interest to me. Book Entity is not / is not part of any relationship. It is simply the "one" entity.
If I pull out the "save lever", every object in the book is saved and I have to delete everything except my desired one.
How do I get around this problem, I really can't find this particular use case in the Master Data Programming Guide, which also hurts me a little, am I going against the best practice here?
Thanks for the help provided.
a source to share
Yes, you are going against the best practices. In this case, if you really don't want to save the objects, keep them in an intermediate format and only create a Core Data object for the one you want to save. However, this seems to be wrong.
What is the harm in keeping other books? The only one that will ever be used?
Based on the small amount of information you have provided, it seems like your approach to the problem needs to be revisited.
Update
Removing unsaved objects from context has very little overhead, so this is the best solution given the options you have. I asked other questions to see if there was a cleaner general solution rather than building objects just to throw them away. It looks like you've already been on this path.
a source to share