我一直在嘗試整個下午讓我的應用程序的CoreData部分工作,並且幾乎讓它工作,因爲它應該。問題是CoreData在應用程序終止時沒有保存適當的值(從多任務菜單中刪除)。剩下的值是我放入實體的intitial值(在我的情況下,定義爲NSString),而不是用戶輸入的新值。所以,問題是節省了應用程序被關閉後永久性的新的價值,尚未出現時,再次應用程序加載初始值永久保存CoreData
實體名稱:Gameinfo
屬性:score
我不知道是什麼我做錯了。如果有人可以幫助我,它將不勝感激。 謝謝!
-(IBAction)saveTheory1
{
AppDelegate *appDelegate= [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Gameinfo"];
NSError *error = nil;
NSArray *someArray = [context executeFetchRequest:request error:&error];
[[someArray objectAtIndex:0] setValue:[self showKey] forKey:@"score"]; // showKey gets the text from UITextField
}
-(IBAction)showTheory1;
{
AppDelegate *appDelegate= [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:@"Gameinfo"
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request
error:&error];
matches = [objects objectAtIndex:0];
NSLog (@"show us the current value: %@", [matches valueForKey:@"score"]);
}
http://stackoverflow.com/questions/10868602/core-data-does-not-update/10868650#10868650 – Adam 2012-08-09 06:55:06