爲了減少冗餘數據,我製作了User的父實體。患者和助產士都是用戶的子女。ManagedObject的子實體的關係給出錯誤的對象
唯一的附加數據是,助產士有一組患者,患者可以有一組助產士。
當我填充的管理對象,以核心數據充分顯示了他們的正確對象的關係。
的問題是,當我嘗試使用用戶類ManagedObject的關係是指向錯誤的對象(它實際上指向同一ManagedObject)檢索對象。
我查詢使用謂詞NSString *predicateString = [NSString stringWithFormat:@"username == '%@' AND password == '%@'", username, password];
當我嘗試使用我的helper方法登錄我檢查,看看是否對象是助產士或病人,所以我知道該叫什麼關係的核心數據。但是這種關係指向了錯誤的對象。
[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搞亂這個嗎?
你在哪裏嘗試訪問關係?你說那是KindOfClass正在爲你工作,並且你說你正在投向User *的某個地方 - 但我沒有看到。 – stevesliva
@ stevesliva對於遲到的回覆感到抱歉。在userWithUsername方法中,我使用問題中顯示的謂詞查詢User類。我不會將它投射到用戶,它會作爲ID傳回。只有我執行了isKindOfClass之後,我纔會將它轉換爲任何類,這樣我就可以調用它的正確方法。即使我沒有使用id類型來使用用戶對象,關係仍然是錯誤的。 –
只是想看看事情出錯了。如果關係的對象respondsToSelector和右側子類的isKindOfClass很難看到你在這裏的代碼有什麼問題。當然你可以調用超類方法。你是否在User類方法中將'self'轉換爲子類?這不一定是個問題,它只是因爲我在這裏看不到它...... – stevesliva