My situation : Assuming I have a Person class (subclass of NSManagedObject). Each time the user clicks the button, a new instance of Person will be created and added to the global NSMutableArray. Also, a new child instance of Person will be added to the child context, for example as follows:
NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateConcurrencyType]; [childContext setParentContext: _mainContext];
Also, when I click the button, I save the context: (this is a little more complicated, but follows this structure)
[childContext performBlock:^{ [childContext save:nil]; [_mainContext save:nil]; }];
After clicking two or more times (not sure if this depends on the total number of clicks), the button objects in my array become fault .
According to the docs: access to the property of the fault object should load the persistent object.
Even when I access the property of my NSManagedObject, the object is still faulty, and the property is nil .
Why is there an error of objects in my array and how do I access the property of the error object?
Edit
When loading the UIViewController, I retrieve all existing objects from the data store:
-(NSArray*)fetchPersons { NSManagedObjectContext *context = [self managedObjectContext]; //this is _mainContext, it is created with initWithConcurrencyType:NSMainQueueConcurrencyType NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *description = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context]; [fetchRequest setEntity:description]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"key = %i", aCondition]; [fetchRequest setPredicate:predicate]; return [context executeFetchRequest:fetchRequest error:nil]; }
I am using this NSArray from fetchPersons to populate NSMutableArray.
Creating a new Person object:
-(Person*)createPerson { NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateQueueConcurrencyType]; [childContext setParentContext:[self managedObjectContext]]; Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:childContext]; return person; }
I am not sure how to handle objects using òbjectID`. I use childContext as a temporary context. Sometimes I need an instance of Person, but I don’t want to store it in persistent storage (or insert it into the main context at the beginning).
After all these steps, I have all the objects in my NSMutableArray. I get the nil properties (error object) after creating some objects and try to write their attributes ( person.name or something else).