简体   繁体   English

应用退出并重新启动时,NSManagedObjectContext不保存

[英]NSManagedObjectContext does not saved when the App Quits & Relaunches

I could not find why the core data NSManagedObjectContext not get saved when my app quits and re-launch. 我找不到为什么我的应用程序退出并重新启动时无法保存核心数据NSManagedObjectContext的原因。 Every time when i quit the app and re-lanch, database is empty 每次我退出应用程序并重新分配时,数据库为空

AppDelegate AppDelegate中

- (void)applicationWillTerminate:(UIApplication *)application
{
[[ShowScreenManager sharedInstance] reset];

// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
[self.tracker set:kGAISessionControl value:@"end"];

} }

- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != 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();
    }
}


}

Save to core data 保存到核心数据

// get manageObjectContext
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName"
                                          inManagedObjectContext:context];

[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
fetchRequest = nil;

if(fetchedObjects.count == 0){
    // insert
    anItem = [NSEntityDescription
                   insertNewObjectForEntityForName:@"SomeName"
                   inManagedObjectContext:context];
}
else{
    // update
    anItem = [fetchedObjects objectAtIndex:0];
}

aSequnce.all = strSequnce;


// save context
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

}

applicationWillTerminate: is basically never used in version of iOS after 4 (when background running came in). applicationWillTerminate:基本上从4(在进入后台运行时)之后的iOS版本中就不再使用。 You should not rely on it for anything. 您不应该依赖它做任何事情。

You should generally be saving the context after updates rather than waiting for app termination. 通常,您应该在更新后保存上下文,而不是等待应用终止。

You can move your logic to applicationDidEnterBackground: but this still won't be called if your app crashes or is terminated from Xcode. 您可以将逻辑移至applicationDidEnterBackground:但是,如果您的应用崩溃或被Xcode终止,则不会调用该逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下按钮时应用退出 - App Quits when button is pressed 当应用程序从后台模式重新启动时,定义自定义启动图像 - Define a custom launch image when app relaunches from the background mode iOS NSManagedObjectContext-保存所有更改后是否可以获取或发送通知? - iOS NSManagedObjectContext - is it possible to get or send a notification when all changes is saved? 应用退出时删除NSUserDefault键值 - Delete NSUserDefault key value when app quits 当用户关闭并打开iPhone设置中的位置服务时,iOS应用会自动重新启动 - iOS app relaunches automatically when user turn off and on the location services in iPhone settings 当应用退出时,Facebook SDK iOS应用保持登录状态 - Facebook SDK iOS app stay logged in when app quits 当用户强制退出应用程序时重新连接蓝牙4.0连接 - Reconnect Bluetooth 4.0 connection when user force quits the app iOS:当应用程序因崩溃而退出时是否有任何委托方法 - iOS: Is there any delegate method when the app quits due to any crash 应用程序意外退出,但在与xcode连接时运行顺畅 - App quits unexpectedly but run smoothly when connected with xcode 当我输入此代码时,应用意外退出(使用核心数据) - App quits unexpectedly when I entered this code (using core data)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM