如何在关闭时让我的应用程序存储信息。因为每次重新打开它都会删除所有核心数据。
以下是一个例子:
NSString *nomville = [[_jsonDict objectAtIndex:indexPath.row] objectForKey:@"label"];
NSString *idVille = [[_jsonDict objectAtIndex:indexPath.row] objectForKey:@"id"];
detailView.title = nomville;
detailView.idVille = idVille;
NSLog(@"Valor: %@", nomville);
NSLog(@"Valor: %@", idVille);
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *villesR = [NSEntityDescription
insertNewObjectForEntityForName:@"Ville"
inManagedObjectContext:context];
[villesR setValue:idVille forKey:@"idV"];
[villesR setValue:nomville forKey:@"nom"];
[self.navigationController pushViewController:detailView animated:YES];
答案 0 :(得分:1)
当您将应用推送到后台或退出时,通常会调用保存方法。
侯可以在你的app delegate中这样做:
- (void)applicationDidEnterBackground:(UIApplication *)application {
[self saveContext];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[self saveContext];
}
#pragma mark - Core Data stack
- (void)saveContext {
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}