核心数据无需明显原因即可恢复为先前状态 [英] Core Data reverts to previous state without apparent reason

查看:67
本文介绍了核心数据无需明显原因即可恢复为先前状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于Core Data的iOS应用程序的一些客户报告说,他们偶尔会丢失数据。这些报告很奇怪,这就是我想请您对此做的原因。客户报告说,他们在一段时间(几分钟,几小时或第二天)后重新打开应用程序时,其某些数据会丢失,就像基础数据库已恢复为以前的状态一样。

A few customers of a Core Data based iOS application report that they occassionally lose data. The reports are very odd, which is the reason I'd like to ask for your take on this. The customers report that when they reopen the application after some time (minutes, hours, or next day), some of their data is lost as if the underlying database reverted to a previous state.

我已经使用Core Data几年了,之前从未遇到过这样的问题。该应用程序非常简单,这意味着我只使用一个受管对象上下文,并且所做的更改在应用程序进入后台之前就已提交。

I have been working with Core Data for several years and have never run in an issue like this before. The application is fairly simple, which means I only use one managed object context and the changes are committed before the application goes to the background.

我意识到这是一个漫长的过程,但是是什么原因可能导致这种类型的问题,或者我可以进行哪些检查以收集更多信息?不幸的是,我无法自己重现该问题,这将使这一切变得容易得多。

I realize that this is a long shot, but what could be some potential causes of this type of problem or what checks can I make to gather more information? Unfortunately, I cannot reproduce the issue myself, which would make all this a lot easier.

更新:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (_persistentStoreCoordinator) return _persistentStoreCoordinator;

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

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@{ NSMigratePersistentStoresAutomaticallyOption : @YES, NSInferMappingModelAutomaticallyOption : @YES } error:&error]) {
        // Error Handling
    }

    return _persistentStoreCoordinator;
}


推荐答案

检查是否有将保存消息放入适当的appDelegate方法中,这样就无法在不保存的情况下退出应用程序。 applicationWillResignActive和applicationWillTerminate应该可以满足您的所有需求。

Check to see if you have put the save message in the appropriate appDelegate methods so that there is not a way to resign the application without saving. applicationWillResignActive and applicationWillTerminate should cover all of your needs.

除了适当的错误处理和日志记录,您还可以从中受益。我个人喜欢将这些类型的错误记录到可以根据请求发送给我的文件中。但这对于您的特定应用程序可能是过大的。这是从内存写入的,因此可以避免任何错误。

Aside from that proper error handling and logging should gain you a lot of ground. I personally like to log these types of errors to a file that can be sent to me upon request. But that might be overkill for your particular application. This is written from memory so forgive any errors.

NSError *error = nil;
if (![[self managedObjectContext] save:&error])
{
    NSString *errorString = @"%@ ERROR : %@ %@", [[NSDate date] description], [error localizedDescription], [error userInfo]);

    NSString *documentsDirectory = [NSHomeDirectory() 
                                    stringByAppendingPathComponent:@"Documents"];

    NSString *filePath = [documentsDirectory 
                          stringByAppendingPathComponent:@"errorLog.txt"];

    // Write to the file
    [errorString writeToFile:filePath atomically:YES 
            encoding:NSUTF8StringEncoding error:&error];
}

这篇关于核心数据无需明显原因即可恢复为先前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆