保存特定对象的managedObjectContext [英] Saving the managedObjectContext for a specific object

查看:365
本文介绍了保存特定对象的managedObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习Core Data,并且在向现有对象添加新对象后更新MOC时遇到问题。我可以创建原始的对象,一个训练日,我可以添加锻炼对象到那个训练日,但我不知道如何保存上下文,所以后来在我的应用程序,我可以在训练日找到所有的练习。

I'm trying to learn Core Data, and am having trouble updating the MOC after adding new objects to an existing object. I can create the original object, a training day, and I can add exercise objects to that training day, but I can't figure out how to save the context so that later in my application I can find all exercises in a training day.

任何想法

这是我的代码:

// 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]);
    }
}


推荐答案

I我有点困惑你的循环,但假设第一个是用于获得每个训练日,设置一些值,然后得到当天的练习,并设置关系尝试以下。

I am a bit confused about your loops but assuming the first one is used to get each Training Day, sets some values and then get the Exercises for that day and set the relationships try the following.

请注意,下面这行代码是创建NSManagedObject的,因此您需要调用它来创建每个TrainingDay对象和每个Exercise对象。

Note that this line of code below is what creates the NSManagedObject, so you need to call it to create each TrainingDay object and each Exercise object.

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

不知道这行是什么,但推测它会创建一个新对象或返回一个如果它已经存在

Not sure what this line does but presumably it creates a new object or returns one if it already exists

VitTrainingDay *trainingDayObject = [VitTrainingDay trainingDayCreateOrObjectWithID:idTrainingDay];

除非它也调用 insertNewObjectForEntityForName 应该用创建NSManagedObject的调用替换,或者搜索并返回具有匹配ID的调用。如果它正在调用 insertNewObjectForEntityForName ,那么你应该删除下面的行,因为只是在数据库中创建另一个 trainingDay 任何属性值。

Unless it is also calling insertNewObjectForEntityForName then it should be replaced with a call that does create the NSManagedObject or searches and returns one with a matching ID. If it is calling insertNewObjectForEntityForName then you should remove the line below because that just creates another trainingDay object in the database without setting any attribute values.

尝试这样

    // 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的现有TrainingDay对象在创建一个新的,所以如果这是调用 [VitTrainingDay trainingDayCreateOrObjectWithID:idTrainingDay]; 然后使用它。

这篇关于保存特定对象的managedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆