发布加载子实体给coredata中的mangedobject [英] Issue loading subentities to mangedobject in coredata

查看:94
本文介绍了发布加载子实体给coredata中的mangedobject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是错误的,它只加载一个汽车零件:
这种方法需要两个数组一个汽车名称,一个零件,创建一个新的汽车,并添加汽车零件,然后保存汽车到核心数据。 (目前不工作这种方式)

I think this is wrong, it only loads one car part: This methods takes two arrays one with car names, one with parts, creates a new car, and adds car parts to it, then saves the car to core data. (currently does not work this way)

for (int i=0; i<[massiveArray count]; i++) {
    //create a new car and part
        Car*newCar =(Car*)[NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];
        CarPart *part =[NSEntityDescription insertNewObjectForEntityForName:@"CarPart" inManagedObjectContext:[self managedObjectContext]];

//set car title to string title in array of titles
        [newCar setValue:[massiveArray objectAtIndex:i] forKey:@"name"];
        //go through car parts array and add all new parts for that specific car
        for (int i=0; i<[partNamesArray count]; i++) {
            [part setValue:[partNamesArray objectAtIndex:i] forKey:@"name"];
            [newCar addToCarPartObject:part];
//save each part??? I think this is wrong
            [self.managedObjectContext save:nil];
        }
//Save new car
        [self.managedObjectContext save:nil];
    }


推荐答案

我知道这里发生了什么。

I know what's going on here.

您需要将新零件对象插入每个零件的核心数据。就像现在,你只做一个部分对象,然后在for循环中覆盖它。你的代码应该看起来像这样...

You need to insert a new part object into core data for each part. As it is right now, you are only making one part object and then overwriting it inside the for loop. Your code should look something akin to this...

for (int i=0; i<[massiveArray count]; i++) {
    //create a new car and part
    Car *newCar = [NSEntityDescription insertNewObjectForEntityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];

    //set car title to string title in array of titles
    [newCar setValue:[massiveArray objectAtIndex:i] forKey:@"name"];

    //go through car parts array and add all new parts for that specific car
    for (int i=0; i<[partNamesArray count]; i++) {
        CarPart *part =[NSEntityDescription insertNewObjectForEntityForName:@"CarPart" inManagedObjectContext:[self managedObjectContext]];
        [part setValue:[partNamesArray objectAtIndex:i] forKey:@"name"];
        [newCar addToCarPartObject:part];
    }
}
//Save the entire context (all pending changes to cars and their parts)
[self.managedObjectContext save:nil];

正如我在上面的评论中所说的,我建议将保存移动到循环外。

As I said in my above comment, I suggest moving the save to outside the loops.

另一个建议更容易阅读的代码。当在一个简单的for循环中枚举数组时,尝试类似于...

Another suggestion for easier to read code. When enumerating an array in a simple for loop try something like...

for (NSString *carTitle in massiveArray) {
    /* Now do your stuff in here... 'carTitle' will be different during 
     * each pass of the loop. No need to increment an i variable or grab 
     * the object from the array on each pass. 
     */
}

这篇关于发布加载子实体给coredata中的mangedobject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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