0

In my app I use two NSManagedObjectContexts, one to store permanent data, the other one just for handling temporary data (so I can work with the NSManagedObject subclasses).

When I execute a fetch request after I created a temporary entity in my temporary NSManagedObjectContext, I get all permanently stored entities AND the temporary entities.

Here is my code:

//create temporarily stored entity
Entity *entity = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:temporaryManagedObjectContext];

//...

//get permanently stored entities
NSMutableArray *mutableFetchResults = [[[permanentManagedObjectContext executeFetchRequest:request error&error] mutableCopy];

Thanks for your help!

Hannes
  • 3,752
  • 2
  • 37
  • 47

1 Answers1

0

Thanks guys, your comments lead me to my error, very stupid I have to say: I copied the code for the creation of the temporary context from the permanent context and forgot to replace all occurances, shame on me!

if (temporaryManagedObjectContext != nil) return temporaryManagedObjectContext;

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    temporaryManagedObjectContext = [[NSManagedObjectContext alloc] init];
    [temporaryManagedObjectContext setPersistentStoreCoordinator:coordinator];
}

//returned the wrong NSManagedObjectContext
return permanentManagedObjectContext;
Hannes
  • 3,752
  • 2
  • 37
  • 47