Iphone CoreData是否永久保存?

Iphone CoreData是否永久保存?,iphone,objective-c,core-data,load,save,Iphone,Objective C,Core Data,Load,Save,我一直在使用iPad应用程序中的核心数据,我可以成功地在应用程序中保存和获取数据。但是,当完全关闭应用程序时,完全退出,将其从多任务中取出,数据就会消失 那么,应用程序关闭时,核心数据是否会将这些数据保存在任何位置?或者我需要找别的地方吗 编辑:这是在app delegatedidfishlaunchingwithoptions:[[UIApplication sharedApplication]delegate]managedObjectContext]中然后我有这样一个:上下文\=[(pro

我一直在使用iPad应用程序中的核心数据,我可以成功地在应用程序中保存和获取数据。但是,当完全关闭应用程序时,完全退出,将其从多任务中取出,数据就会消失

那么,应用程序关闭时,核心数据是否会将这些数据保存在任何位置?或者我需要找别的地方吗

编辑:这是在app delegate
didfishlaunchingwithoptions
[[UIApplication sharedApplication]delegate]managedObjectContext]中然后我有这样一个:
上下文\=[(prototypeAppDelegate*)[[UIApplication sharedApplication]委托]managedObjectContext]

这是在应用程序委托中预先生成的NSPersistentStoreCoordinator代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"prototype.sqlite"];

    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.

         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.


         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.

         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

         * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
         [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.

         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}
到目前为止,我正在使用它获取数据:

    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *testEntity = [NSEntityDescription entityForName:@"DatedText" inManagedObjectContext:context_];
    [fetch setEntity:testEntity];
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"dateSaved == %@", datePicker.date];
    [fetch setPredicate:pred];

    NSError *fetchError = nil;
    NSArray *fetchedObjs = [context_ executeFetchRequest:fetch error:&fetchError];
    if (fetchError != nil) {
        NSLog(@"fetchError = %@, details = %@",fetchError,fetchError.userInfo);
    }
    noteTextView.text = [[fetchedObjs objectAtIndex:0] valueForKey:@"savedText"];
要保存数据,请执行以下操作:

NSManagedObject *newDatedText;
    newDatedText = [NSEntityDescription insertNewObjectForEntityForName:@"DatedText" inManagedObjectContext:context_];
    [newDatedText setValue:noteTextView.text forKey:@"savedText"];
    [newDatedText setValue:datePicker.date forKey:@"dateSaved"];

    NSError *saveError = nil;
    [context_ save:&saveError];
    if (saveError != nil) {
        NSLog(@"[%@ saveContext] Error saving context: Error = %@, details = %@",[self class], saveError,saveError.userInfo);
    }

您应该发布设置NSPersistentStoreCoordinator并添加NSPersistentStore的代码。您是否正在使用
nsimemorystoretype
作为您的店铺类型?因为这会导致你看到的行为。或者,您每次都可以使用不同的路径访问商店,这将为您每次提供一个新的商店。一般来说,您的存储应该位于Documents文件夹中,并且在每次启动时都应该为其指定相同的名称。它还应该使用
NSSQLiteStoreType

是否将上下文保存在正确的位置?当只在willTerminate中输入后台应用程序状态时,不保存上下文是一个常见错误

在以下appdelegate方法中保存上下文:

-(void)applicationDidEnterBackground:(UIApplication *)application
插入对象后直接保存上下文,这就足够了。保存后,在模拟器中检查sqlite文件是否包含任何数据

如果

如果在上下文中找到对象,则不会引发异常。可能它不包含期望值?
将fetchrequest返回的对象记录到控制台,以查看是否是这种情况

我发现了问题。事实证明,由于使用UIDatePicker,在程序开始时,它使用以下命令将日期选择器设置为今天:

NSDate *now = [[NSDate alloc] init];
[datePicker setDate:now];
因此,如果不使用此功能,它将非常有效。因此,目前我正在寻找解决这个问题的方法,因为这条线似乎导致了这个问题


如果在创建项目后添加CoreData,则在NSManagedObject中存在出错的风险

-(NSManagedObjectContext*) managedObjectContext{
if (!_managedObjectContext) {
    _managedObjectContext =[self createManageObjectContextWithName:@"name.sqlite"];
}
return _managedObjectContext;}
以下是正确的方法:

- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
    return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
    return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;}

如何设置managedObjectContext?为了在退出后保存数据,您需要设置NSPersistentStoreCoordinator。更新了问题并提供了更多详细信息。您的保存代码完全错误。除非调用
-save:
返回否,否则绝对不能查看
saveError
的值。这样做是编程错误,可能导致应用程序崩溃
-save:
不保证该变量保留有效值,除非它返回NO。因此,如果(![context\uuusave:&saveError]){/*read saveError*/}
,则应使用
。更新了问题的详细信息。我不明白。当应用程序进入后台时,您正在加载便条?我想我的回答不清楚。1.在ApplicationIdentinterBackground中添加保存代码。2.在我的回复中提到的代码行中添加日志记录。
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
    return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
    return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;}