开发者

NSManaged Object Loop saving same row multiple times

开发者 https://www.devze.com 2023-03-31 02:56 出处:网络
I\'m trying to write data from an NSMutableArray into a core data table via an NSManagedObject for loop. It writes out the last record in the the array multiple times rather than writing out each of t

I'm trying to write data from an NSMutableArray into a core data table via an NSManagedObject for loop. It writes out the last record in the the array multiple times rather than writing out each of the distinct rows in the array.

I've done a fast enumeration loop on the array to confirm it has multiple distinct rows.

This is the current version of my code loop:

  //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" 
         开发者_如何学Python                                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

Thoughts on why it's stuck on the last record in the array?


This is because circle

for (NSManagedObject *info in fetchedObjectsForAttributes) {

saves only last object, as it rewrites all previously set data. Just replace that loop with that one:

NSManagedObject *info = (NSManagedObject *)object;

And all will be ok as circle

for (id object in fetchedObjectsForAttributes ) {

will iterate objects one by one.


You've only called insertNewObjectForEntityForName:etc. once, so of course you only have one new object. You set its attributes to the values from each of your items in the list, but each time through the loop it overwrites the values from the previous time. It ends up with the values from the last item.

Instead just move the whole line

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

into the loop, and it should be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消