Question

I parse an xml file containing books, for each new node I go:

Book *book = (Book*)[NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:managedObjectContext];

To obtain an NSManagedObject of my Core Data Book Entity, I then proceed to populate the managed Book object with data, add it to an array, rinse, repeat.

When I am done, I present the list of books to the user. I have not yet executed the save:

NSError *error;
if (![managedObjectContext save:&error]) {
    NSLog(@"%@", [error domain]);
}

The user now selects one of the books, this one I would like to persist, but only this one, all the other books are of no interest to me any more. The Book Entity does not have/or is part of any relationships. It is just a "single" Entity.

If I pull the "save lever" every Book object will be persisted and I will have to delete everything but my desired one.

How would I get around this challenge, I can't really seem to find that particular use-case in the Core Data Programming Guide, which sort of also bugs me a bit, am I going against best practice here?

Thanks for any help given.

Was it helpful?

Solution

Yes you are going against best practices. In that use case, if you really do not want to save the objects, keep them in an intermediate format and only create a Core Data object for the one you want to persist. However this seems quite wrong.

What is the harm in saving the other books? Are the only going to be used once ever?

Based on the small amount of information you have provided, it seems your approach to the problem needs to be reconsidered.

Update

Removing unsaved objects from the context has a very small overhead yes so that is the best solution given the parameters you have. I asked the other questions to see if there was a cleaner overall solution rather than building the objects just to throw them away. Sounds like you have already been down that path though.

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