I have two NSManageObjects, Midwife
and Patient
. These both have the parent entity of User
. The only difference is that a Midwife
object has a many to many relationship to Patient
and the Patient
object has a many to many relationship to Midwife
When I get the active user it correctly returns the user and identifies if it is a Midwife
or a Patient
. You can see with the image below that it correctly retrieves the object I want with the patients relationship faulted.
Using the code below I determine that the relationship is a fault then try to access a property to make core data fulfil the fault. I then add the object to a mutable array to be used late in a collection view.
[User activeUserSuccess:^(id user) {
self.activeMidwife = (Midwife *)user;
self.patients = [@[] mutableCopy];
[[[_activeMidwife patients] allObjects] enumerateObjectsUsingBlock:^(Patient *patient, NSUInteger idx, BOOL *stop) {
NSLog(@"Is fault : %i", [patient isFault]);
[patient fullName];
NSLog(@"Is fault : %i", [patient isFault]);
[_patients addObject:patient];
}];
} failure:^(NSError *error) {
NSLog(@"Error : %@", [error description]);
}];
The first NSLog
returns "Is fault : 1"
and then once I call the fullName
method (this simply places the forename and surname property together with a space) the object is then loaded and the second NSLog outputs "Is fault : 0"
Whilst still in the enumerate block I print out the patient.mangaedObjectContext
and here it returns a valid managedObjectContext
it then adds it to the mutable array and does this fall all patents for the active midwife.
The problem is when I access the _patents
array from within the - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
method the managedObjectContext is nil. My code is below.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MDCCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
Patient *patient = _patients[indexPath.row];
NSLog(@"Is fault : %i", [patient isFault]);
NSLog(@"Patitne name %@", patient.forename);
NSLog(@"Is fault : %i", [patient isFault]);
[[cell patientNameLabel] setText:[patient fullName]];
return cell;
}
Within these two NSLog
s they both output "Is fault : 1"
. When debuting I found that the patent object at this point the patent
object has a nil managedObjectContext
. Would anyone know why or accord this problem before.
My persistentStore
is only init
once and accessed via a sharedInstance
.
Thanks for the help.
Quick answer to reflect comments. If the MOC goes out of scope, then it makes sense the entity instance pointers that you've retained have internal pointers to a MOC that's now nil.
Beyond that, I don't know what is supposed to happen with the usability or mutability of these NSManagedObjects that are now retained outside the scope of the MOC you got them from. So there's potentially a more informed answer about what you could or could not do with that array you've created.