为减少冗余数据,我创建了User的父实体。患者和助产士都是用户的孩子。
唯一的补充数据是,助产士有一组患者,患者可以有一组助产士。
当我将托管对象填充到核心数据时,它会显示它们与正确对象的关系。
问题是,当我尝试使用User类检索对象时,ManagedObject的关系指向错误的对象(它实际上指向相同的ManagedObject)。
我使用谓词NSString *predicateString = [NSString stringWithFormat:@"username == '%@' AND password == '%@'", username, password];
当我尝试使用我的助手方法登录时,我会检查该对象是否是助产士或患者,所以我知道要打电话的关系。但这种关系指向了错误的对象。
[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,但这是我的数据及其关系破坏的地方。
我认为这是因为我正在将一个助产士管理对象转换为用户管理对象,但我该怎么办呢?
提前致谢。
修改
我像这样执行提取请求
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];
}];
可能是entityDescription弄乱了吗?
答案 0 :(得分:0)
原来没有问题。只有当我NSLog的user.patients属性时才会出现故障。如果我通过它们进行枚举,我会得到正确的整个对象。
感谢大家的帮助。