How to not persist NSManagedObjects retrieved from NSManagedObjectContext

Posted by RickiG on Stack Overflow See other posts from Stack Overflow or by RickiG
Published on 2010-04-20T16:40:13Z Indexed on 2010/04/20 16:43 UTC
Read the original article Hit count: 521

Hi

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.

© Stack Overflow or respective owner

Related posts about core-data

Related posts about nsmanagedobject