我整个下午都在努力让我的应用程序的 CoreData 部分正常工作,并且几乎让它正常工作。问题是当应用程序终止(从多任务菜单中删除)时,CoreData 没有保存适当的值。剩下的值是我放入实体的初始值(在我的例子中为 0,定义为 NSString),而不是用户输入的新值。所以问题是在应用程序关闭后永久保存新值,并且在加载应用程序时不再显示初始值
实体名称: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"]);
}