I'm writing an application that uses a UIPickerView and is storing each change to the pickers in core data (the name of the app is Wizard Blood in the iTunes store). I've had some success so far in that my -didSelectRow method successfully adds new objects to core data via the following code:
// Get context, entity, managedobject from the fetchedResultsController
[newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
//Iterates through each component and sets the value to the appropriate key
for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
[newManagedObject setValue:[NSNumber numberWithInteger:[LifeCounter selectedRowInComponent:j]] forKey:keyName];
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Error saving entity: %@", [error localizedDescription]);
}
However, in my -viewDidLoad I can't seem to get core data to load values from the latest object. This is what I have so far:
lifeCounterArray = [[NSMutableArray alloc] init];
for ( int i = 200; i >= -200; i-- )
{
NSString *myString = [NSString stringWithFormat:@"%i", i];
[lifeCounterArray addObject:myString];
}
//Hopefully this retrieves the latest values from the core data store
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//Iterates through each component, setting it to the stored value in core data
for (int j=0; j < [LifeCounter numberOfComponents]; j++) {
NSString *keyName = [NSString stringWithFormat:@"lifeTotal%i",j];
NSInteger row = [[managedObject valueForKey:keyName] integerValue] +180;
[LifeCounter selectRow:row inComponent:j animated:NO];
}
Now, I'm pretty sure my fetchedResultsController method is working since I can see it creating objects (I can see them in the sqlite database) but maybe I shouldn't even be using a fetchedResultsController since I really only care about the latest object. I'd be happy to post my fetchedResultsController method if needed, thanks for the help in advance.