我一直在試圖獲得核心數據實體間工作關係的單一任務。我已經實現了這一點,但現在我需要對其進行更改,以便新屬性值將其關係添加到現有對象中。它是一對多的數據庫。在覈心數據中添加關係
我不知道如何將關係添加到已存在的對象。因此,在添加到RoutineDetail的新對象中,我將如何創建與例程實體中已存在的對象的關係?
我看過幾個例子都顯示如何添加關係到新添加的對象,但我需要它,因此RoutinesDetails
中的新對象與Routines
中已存在的值有關係。
的Routines
值在一個字符串舉行名爲RoutineText
rout is the NSmangedObject for the entity Routines
routDet is the NSmanagedObject for the entity RoutinesDetails
我已經離開了註釋掉的代碼,讓我創建了兩個新對象時添加的關係。
這是我不得不在我的項目中做的最後一件事,但它讓我瘋狂。我會永遠感謝這裏的修復。任何建議將不勝感激,因爲這是最好的知識門戶。謝謝。
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new device
ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
//rout = [NSEntityDescription insertNewObjectForEntityForName:@"Routines" inManagedObjectContext:context];
routdet = [NSEntityDescription insertNewObjectForEntityForName:@"RoutinesDetails" inManagedObjectContext:context];
//Add attribute values
//[rout setValue: RoutineText forKey:@"routinename"];
[routdet setValue: info.name forKey:@"image"];
//Create Relationship
[rout addRoutinedetObject:routdet];
什麼是不工作? – Mundi
昨天你問的不是這個問題嗎? http://stackoverflow.com/questions/17686651/core-data-relationship-add-to-existing-value –