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"
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
saves only last object, as it rewrites all previously set data.
Just replace that loop with that one:
And all will be ok as circle
will iterate objects one by one.