当模型发生变化时,擦除所有存储在 CoreData 中的数据 [英] Wipe all data stored with CoreData when the model has changed

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

问题描述

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

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

因为我使用 Core Data,所以每次我的架构更改时,当我尝试使用存储在设备上的先前数据运行应用程序时,它都会崩溃.检测此更改并擦除设备中所有数据的最快方法是什么,因为我不介意重新下载所有数据.它击败了崩溃并将架构重新映射到新架构(在我的情况下).

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).

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

I see that this check is performed in the getter:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator

所以我只需要知道用于擦除整个数据库并再次设置 Core Data 的方法.谢谢:)

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

推荐答案

回到这个问题,为了从我的 CoreData 存储中删除所有数据,我决定简单地删除 sqlite 数据库文件.所以我只是像这样实现了 NSPersistentStoreCoordinator:

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天全站免登陆