ManagedObject的子实体的关系给出错误的对象

为了减less冗余数据,我做了User的父实体。 患者和助产士都是用户的子女。

唯一的附加数据是,助产士有一套病人,病人可以有一套助产士。

导入时管理对象的屏幕截图

当我将pipe理对象填充到核心数据时,它会显示它们与正确对象的关系。

核心数据模型的屏幕截图

问题是,当我尝试使用User类检索对象时,ManagedObject的关系指向错误的对象(它实际上指向同一个ManagedObject)。

我使用谓词查询核心数据NSString *predicateString = [NSString stringWithFormat:@"username == '%@' AND password == '%@'", username, password];

当我尝试使用我的帮助器方法login时,我检查是否该对象是一个助产士或患者,所以我知道要调用什么关系。 但是这种关系指向了错误的对象。

 [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"]; }); }]; 

它知道对象是什么类,并调用正确的segue,但这是我的数据和他们的关系变得腐败的地方。

我认为这是因为我正在将一个助产士托pipe对象转换为一个用户托pipe对象,但是我该怎么做?

提前致谢。

编辑

我这样执行获取请求

 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]; }]; 

难道这是实体描述搞砸了吗?

原来没有问题。 只有当我NSLog的user.patients属性它是有问题的。 如果我通过他们枚举我得到正确的和整个对象。

感谢大家的帮助。