Relationships of a child entity of ManagedObject gives wrong object

て烟熏妆下的殇ゞ 提交于 2020-01-06 12:52:08

问题


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.



来源:https://stackoverflow.com/questions/22993233/relationships-of-a-child-entity-of-managedobject-gives-wrong-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!