Relationships of a child entity of ManagedObject g

2019-09-02 18:21发布

问题:

To reduce redundant data I have made a parent entity of User. Both patient and Midwife are children of User.

The only additional data is that a Midwife has a set of patients and a Patient can have a set of midwifes.

When I am populating the managed objects to core data it shows that their relationships to the correct objects.

The problem is, when I try to retrieve the object using the User class the relationship of the ManagedObject is pointing to the wrong object (it actually points to the same ManagedObject).

I query core data using the predicate NSString *predicateString = [NSString stringWithFormat:@"username == '%@' AND password == '%@'", username, password];

When I attempt to login using my helper method I check to see if the object is a Midwife or a Patient so I know what relationship to call. But the relationship is pointing to the wrong object.

[User userWithUsername:username
              password:password
               success:^(id user) {                
                   if([user isKindOfClass:[Midwife class]])
                   {
                       NSLog(@"Midwife : %@", [user fullName]);
                       dispatch_sync(dispatch_get_main_queue(), ^{
                           [self performSegueWithIdentifier:@"midwifeDashboard" sender:self];
                       });
                   }
                   else if([user isKindOfClass:[Patient class]])
                   {
                       NSLog(@"Patient : %@", [user fullName]);
                       dispatch_sync(dispatch_get_main_queue(), ^{
                           [self performSegueWithIdentifier:@"patientDashboard" sender:self];
                       });
                   }
               }
               failure:^(NSError *error) {
                   NSLog(@"Error %@", [error description]);
                   dispatch_sync(dispatch_get_main_queue(), ^{
                       [SVProgressHUD showErrorWithStatus:@"Invalid username or password"];
                   });
               }];

It knows what class the object is and calls the correct segue but this is where my data and their relationships become corrupt.

I assume it is because I am casting a Midwife Managed Object to a User Managed Object but how can I do this?

Thanks in advance.

Edit

I perform the fetch request like so

NSManagedObjectContext* context = [[[MDCDataStore sharedInstance] store] newPrivateContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];

[context performBlock:^{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:entityDescription];
    [fetchRequest setPredicate:predicate];
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:nil];
}];

Could it be the entityDescription messing this up?

回答1:

Turns out there was no problem. It was only that when I NSLog the user.patients property it is faulted. If I enumerate through them I get the correct and entire object.

Thanks everyone for your help.