Reputation: 694
How I can make my app store the information when it is closed. Because each time I reopen it, all the Core Data is erased.
Here is an example:
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];
Upvotes: 0
Views: 134
Reputation: 69469
You would normally call the save methods when your app is pushed to background or exited.
Hou can do it like this in your 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();
}
}
}
Upvotes: 1