我正在尝试学习核心数据,并且在向现有对象添加新对象后无法更新MOC。我可以创建原始对象,训练日,我可以在训练日添加锻炼对象,但我无法弄清楚如何保存上下文,以便稍后在我的应用程序中我可以在训练日找到所有练习。
任何想法??
这是我的代码:
// 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]);
}
}
答案 0 :(得分: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");
}
我猜您需要在创建新ID之前搜索具有相同ID的现有TrainingDay对象,因此,如果这就是此调用[VitTrainingDay trainingDayCreateOrObjectWithID:idTrainingDay];
所做的,则使用它。