NSManagedObject循环多次保存同一行

时间:2011-08-28 17:29:45

标签: iphone core-data ios4 nsmutablearray nsmanagedobject

我正在尝试通过NSManagedObject for循环将NSMutableArray中的数据写入核心数据表。它多次写出数组中的最后一条记录,而不是写出数组中的每个不同的行。

我在数组上做了一个快速枚举循环,以确认它有多个不同的行。

这是我的代码循环的当前版本:

  //see if there were any matching rows from All_Game_Tips_List entity and of course there should be 
if (fetchedObjectsForAttributes == nil) {
    // do nothing as user1 does not have a saved profile
    NSLog(@"error no matching rows found which sounds suspect");
}
else 
{
    for (id object in fetchedObjectsForAttributes ) {
        NSLog(@"alltip_obj = %@", object);

    NSLog(@"found exactly %i matching alltip records",[fetchedObjectsForAttributes count]);                                                                                  

//next need to write a couple of fields from the profile entity and some from All_Game_Tips_List entity to mytips table  but first need to get all needed attributes for an attribute (e.g. name, tminus, etc) for an attribute
//then insert the new row
NSManagedObjectContext *contextForMyTips = [appDelegate managedObjectContext];   


NSManagedObject *myTipsFromAllTips = [NSEntityDescription
                                         insertNewObjectForEntityForName:@"My_Game_Tips_List" 
                                         inManagedObjectContext:contextForMyTips];
NSLog(@"start wri to mytips");

for (NSManagedObject *info in fetchedObjectsForAttributes) {
    [myTipsFromAllTips setValue:[info valueForKey:@"alltip_name"]               forKey:@"mytip_name"];
    [myTipsFromAllTips setValue:[info valueForKey:@"alltip_alert_msg"]          forKey:@"mytip_alert_msg"];
    [myTipsFromAllTips setValue:[info valueForKey:@"alltip_description"]        forKey:@"mytip_description"];
    [myTipsFromAllTips setValue:[info valueForKey:@"alltip_id"]                 forKey:@"mytip_id"];
    [myTipsFromAllTips setValue:[info valueForKey:@"alltip_tminus_amt"]         forKey:@"mytip_tminus_amt"];
    [myTipsFromAllTips setValue:[info valueForKey:@"alltip_impact_type"]        forKey:@"mytip_impact_type"];


}    // end of for NSManagedObject loop

        //commit the insert
        if (![contextForMyTips save:&error]) {
            NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
        }
}    // looping through id 

}    // end of else

为什么它会卡在数组中的最后一条记录上的想法?

2 个答案:

答案 0 :(得分:0)

这是因为圈子

for (NSManagedObject *info in fetchedObjectsForAttributes) {

仅保存最后一个对象,因为它会重写所有先前设置的数据。 只需用那个循环替换该循环:

NSManagedObject *info = (NSManagedObject *)object;

一切都可以圆圈

for (id object in fetchedObjectsForAttributes ) {

将逐个迭代对象。

答案 1 :(得分:0)

您只调用了insertNewObjectForEntityForName:etc.一次,所以当然您只有一个新对象。您将其属性设置为列表中每个项目的值,但每次循环时它都会覆盖上一次的值。它最终得到了最后一项的值。

而只是移动整行

NSManagedObject *myTipsFromAllTips = [NSEntityDescription
                                     insertNewObjectForEntityForName:@"My_Game_Tips_List" 
                                     inManagedObjectContext:contextForMyTips];

进入循环,它应该没问题。