当模型更改时,擦除使用CoreData存储的所有数据 [英] Wipe all data stored with CoreData when the model has changed

查看:83
本文介绍了当模型更改时,擦除使用CoreData存储的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序从互联网获取数据,并使用CoreData将其存储在设备中,以获得更流畅的体验。



因为我使用Core Data,时间我的模式更改,应用程序崩溃时,我试图运行它与存储在设备上的以前的数据。什么是最快的方式来检测这种变化,并擦除设备中的所有数据,因为我不介意重新下载所有的。



我看到这个检查是在getter中执行的:

   - (NSPersistentStoreCoordinator *)persistentStoreCoordinator 

所以我只需要知道实现擦除整个数据库和重新设置Core Data的方法。回到这个问题,要从我的CoreData存储中删除所有的数据,我决定:简单的删除sqlite数据库文件。所以我只是这样实现 NSPersistentStoreCoordinator

   - (NSPersistentStoreCoordinator * )persistStoreCoordinator {

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

NSURL * storeUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] ​​stringByAppendingPathComponent:@myAppName.sqlite]];

NSError * error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:& error]){

NSLog(@打开数据库时出错再试一次。

//删除sqlite文件,然后重试
[[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:& error]){
NSLog(@Unresolved error%@,%@,error,[error userInfo]);
abort();
}

//如果应用程序没有退出,显示警告通知用户数据已被删除
UIAlertView * alert = [[[UIAlertView alloc] initWithTitle :@读取数据库时遇到错误,请允许所有数据重新下载。 message:@delegate:nil cancelButtonTitle:@OKotherButtonTitles:nil] autorelease];
[alert show];
}

return persistentStoreCoordinator;
}


I have an app that fetches data from the internet and uses CoreData to store them in the device, for a smoother experience.

Because I use Core Data, every time my schema changes, the app crashes when I try to run it with the previous data stored on the device. What is the fastest way to detect this change and wipe all the data from the device, since I don't mind redownloading them all. It beats crashing and remapping the schema to the new one (in my case).

I see that this check is performed in the getter:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

so I only need to know the methodology to implement for wiping the whole database and seting up Core Data again. Thanks :)

解决方案

Coming back to this question, to delete all the data from my CoreData storage I decided to simple delete the sqlite database file. So I just implemented the NSPersistentStoreCoordinator like this:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

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

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"myAppName.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {

        NSLog(@"Error opening the database. Deleting the file and trying again.");

        //delete the sqlite file and try again
        [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }

        //if the app did not quit, show the alert to inform the users that the data have been deleted
        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error encountered while reading the database. Please allow all the data to download again." message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
        [alert show];
    }

    return persistentStoreCoordinator;
}

这篇关于当模型更改时,擦除使用CoreData存储的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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