2013-12-15 63 views
0

我正在嘗試學習Core Data,並且在將新對象添加到現有對象後更新MOC時遇到問題。我可以創建原始對象,一個訓練日,並且我可以將訓練對象添加到該訓練日,但我無法弄清楚如何保存上下文,以便稍後在我的應用程序中可以在訓練日中找到所有練習。保存特定對象的managedObjectContext

任何想法??

這裏是我的代碼:

// Data from JSON 
NSArray *trainingDayData = responseData[@"training_days"]; 
for (NSDictionary *aTrainingDay in trainingDayData) { 

    // Find the specific training day and save the MOC, creating the trainingDayObject 
    NSNumber *idTrainingDay = [NSNumber numberWithInt:[[aTrainingDay objectForKey:kID_KEY] intValue]]; 
    VitTrainingDay *trainingDayObject = [VitTrainingDay trainingDayCreateOrObjectWithID:idTrainingDay]; 

    // Configure the VitTrainingDay object's fields 
    trainingDayObject.name = aTrainingDay[@"name"]; 
    trainingDayObject.order = aTrainingDay[@"order"];    

} 
// assign exercises to each trainingDayObject(this is inside a larger for loop) 
trainingDayObject.userExercise = [NSSet setWithArray:userExerciseObjects]; 

// Below are attempt one and two to update the MOC after assigning exercises to the trainingDayObject. 

// This works to save the updated MOC, but also adds two blank trainingDayObjects, since it 'insertNewObjectForEntityName', which I don't want. 
trainingDayObject = [NSEntityDescription insertNewObjectForEntityForName:@"TrainingDay" inManagedObjectContext:self.context]; 

//This as far as I can tell is doing nothing. It just points to the conventional MOC save method. I pull it out below. 
[self.coreDataManager saveContextForManagedObjectContext:self.context]; 

這裏是saveContextForManagedObjectContext方法我稱之爲上面:

- (void)saveContextForManagedObjectContext:(NSManagedObjectContext *)moc 
{ 
    NSError *error = nil; 
    if (![moc save:&error]) { 
     NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
    } 
} 

回答

1

我有點困惑你的循環,但假設第一個是用來獲取每個培訓日,設置一些值,然後獲得當天的練習,並設置關係嘗試以下。

請注意,下面這行代碼是創建NSManagedObject的,因此您需要調用它來創建每個TrainingDay對象和每個Exercise對象。

trainingDayObject = [NSEntityDescription insertNewObjectForEntityForName:@"TrainingDay" inManagedObjectContext:self.context]; 

不知道這行做,但據推測它會創建一個新的對象,如果它已經存在

VitTrainingDay *trainingDayObject = [VitTrainingDay trainingDayCreateOrObjectWithID:idTrainingDay]; 

除非還呼籲insertNewObjectForEntityForName那麼它應該有一個調用,它被替換返回一個創建NSManagedObject或者搜索並返回一個具有匹配ID的。如果它調用insertNewObjectForEntityForName,則應刪除下面的行,因爲它只是在數據庫中創建另一個trainingDay對象而不設置任何屬性值。

嘗試這樣的事情

// Data from JSON 
    NSArray *trainingDayData = responseData[@"training_days"]; 

    for (NSDictionary *aTrainingDay in trainingDayData) { 

     // Find the specific training day ID 
     NSNumber *idTrainingDay = [NSNumber numberWithInt:[[aTrainingDay objectForKey:kID_KEY] intValue]]; 

     //Create the Core Data Object 
     //Assume VitTrainingDay is a NSManagedObject subclass 
     VitTrainingDay *trainingDayObject = [NSEntityDescription insertNewObjectForEntityForName:@"TrainingDay" inManagedObjectContext:self.context]; 

     // Set the attributes 
     trainingDayObject.ID = idTrainingDay; 
     trainingDayObject.name = aTrainingDay[@"name"]; 
     trainingDayObject.order = aTrainingDay[@"order"];    


     // assign exercises to each trainingDayObject 
     for (SomeSourceObject *object in SomeExercisesSource) { 
      ExerciseObject *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"ExerciseObject" inManagedObjectContext:self.context]; 

      // Set the exercises parent object (training day) 
      exercise.trainingDay = trainingDayObject; 
      exercise.details = object.details; 

     } 

    } 


NSError *error = nil; 
if (![self.context save:&error]) { 
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
} else { 
    NSLog(@"Save successful"); 
} 

我的猜測是,你需要創建一個新的,所以如果那是什麼這個電話[VitTrainingDay trainingDayCreateOrObjectWithID:idTrainingDay];不會再使用它,而不是之前尋找具有相同ID的現有震撼教育對象。

+0

我仍然遇到同樣的問題。它按預期工作,如果我設置了一個斷點,我可以看到練習被分配到訓練日,並且調用了「NSLog(@」保存成功「);',但練習沒有被保存到出於某種原因的培訓日期。我已經成功地通過使用'trainingDayObject = [NSEntityDescription entityForName:@「trainingDay」inManagedObjectContext:self.context]獲得了保存到訓練日的練習;'但是這給了我警告:'不兼容的指針類型分配給'VitTrainingDay * '來自'NSEntityDescription *'' – Arel

+0

發佈VitTrainingDay –

+0

的定義'VitTrainingDay'是'TrainingDay'核心數據的一個'NSManagedObject'子類實體 – Arel

相關問題