@
everbird @
jjgod 昨天弄了一晚上。。首先发现是我测试的问题。因为我是insert以后 就直接Cmd-R来重新运行程序。所以,不是正常退出程序。发现这个问题以后。
目前 解决方法
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
[self saveContext];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[self saveContext];
}
- (void)saveContext
{
OMPCoreDataManager *dataManager = [OMPCoreDataManager shareOMPCoreDataManager];
NSManagedObjectContext *managedObjectContext = dataManager.ompDatabase.managedObjectContext;
if (managedObjectContext != nil) {
[managedObjectContext performBlock:^{
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}];
}
}
用了类似这样的解决方法。看stackoverflow上说并不能保证100%的正确性。不够我测试了一下真机上 应该没有问题。