Reputation: 141
I'm trying to add data into an attribute of a core data entity.
Contents of contentArray: (
036,
038,
040,
041,
043,
044,
044,
043,
042,
041,
041,
042,
042,
042,
042,
041,
041,
042,
043,
044,
045,
046,
047,
048,
050,
053,
054,
056,
059,
060,
057,
055,
053,
051,
048,
046,
043,
035,
034,
033,
032,
031,
032
}
Given above is the sample contents of the mutable array. I'm trying to add it into a NSManagedObjectContext variable as given below:
for (int i =0;i<[contentArray count];i++){
int a =[[contentArray objectAtIndex:i] intValue];
NSLog(@"value:%d",a);
[newManagedObject setValue:[NSNumber numberWithInteger:a ] forKey:@"timeStamp"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
I just manipulated the default core data app for iOS where it adds a timestamp value to the table. The cells is being over written with previous value. Is there anything wrong with my implementation. How to add data rather than over writing datas of the content array? Any help would be greatly appreciated. Thanks
Upvotes: 0
Views: 414
Reputation: 8501
This is a basic way to store data into core data..Check out this and change it for your code...
NSManagedObjectContext *context = [self managedObjectContext];
countryObject=[NSEntityDescription
insertNewObjectForEntityForName:@"Country"
inManagedObjectContext:context];
countryObject.city = @"New york";
countryObject.people = [NSString stringWithFormat:@"%@",[yourArray objectAtIndex:i]];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
Upvotes: 0
Reputation: 135548
You have to create a new managed object (and add it to your context) in every pass through the loop. As long as you always use the same object it's no wonder that your data gets overwritten.
On the other hand, you should probably put the saving code outside the loop.
Upvotes: 2