I'm working with core data to save scores for a game, and need help updating an entry when a score is beaten. This is where I'm at. I write the data the first time the app launches like this:
LevelData *levelOne = [NSEntityDescription insertNewObjectForEntityForName:@"LevelData" inManagedObjectContext:context];
levelOne.levelNum = @"1";
levelOne.topScore = @"0";
levelOne.isPassed = @"No";
if (![context save:&error]) {
NSLog(@"coudlnt save: %@", [error localizedDescription]);
}
I then read out the data at the end of the level like this:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"levelNum == 1"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"LevelData" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (LevelData *info in fetchedObjects) {
NSLog(@"Is Passed: %@", info.levelNum);
NSLog(@"Top Score: %@", info.topScore);
NSLog(@"Is Passed: %@", info.isPassed);
}
What I'm stuck doing is updating the topScore entry and writing it back to the data store without creating a new entry, but updating the existing one.
And help/example would be so so helpful and much appreciated.
Thanks, Kyle