如何简单地删除旧的核心数据并重建新的? [英] How to simply delete old core data and rebuilt the new one?

查看:62
本文介绍了如何简单地删除旧的核心数据并重建新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将coreData迁移到新版本,我正在执行以下步骤:


添加新的模型版本(选择名称。如果您已经有
的应用程序已提交到App Store,并且正在使用较早的模型版本,请先进行xcdatamodeld然后编辑器->添加
模型版本)进行更改。 $ b

然后,从核心数据选项卡中添加一个新文件,作为映射模型选择,
源模型(提交的应用程序正在使用的模型版本)
目标模型(您拥有的模型版本)完成更改)




但是我的数据主要是图像和应用崩溃,因为占用大量内存。因此,我想在用户更新其应用程序时删除旧数据模型及其数据并创建空的新模型数据。

如果数据模型发生更改,您只需检查数据库文件具有的模型即可。如果不是新文件,请使用 NSFileManager 删除StoreCoordinator中指定的文件,然后再次初始化StoreCoordinater和 NSManagedContext



类似这样的东西(未经测试的代码):

  var错误:NSError 
var applicationDocumentsDirectory:NSURL = NSFileManager.defaultManager()。URLsForDirectory(NSDocumentDirectory,inDomains:NSUserDomainMask).lastObject
let storeURL:NSURL = applicationDocumentsDirectory.URLByAppendingPathComponent( Database.sqlite )
NSFileManager.defaultManager()。removeItemAtPath(storeURL.path,error)






Swift 4更新:

 
{
var applicationDocumentsDirectory:URL = FileManager.default.urls(for:.documentDirectory,in:.userDomainMask).last!
让storeURL:URL = applicationDocumentsDirectory.appendingPathComponent( Database.sqlite)
试试FileManager.default.removeItem(atPath:storeURL.path)
}
catch
{
print(error.localizedDescription)
}






如果模型未更改,则需要将更新信息保存在任何位置。数据库本身或UserDefaults中的文本文件。您只需要一个标志来检查数据库是否已更新/清除。



然后,您也可以像上面一样删除数据库或获取所有对象并删除它们。 / p>

I tried to migrate coreData to new version, I'm following this step:

Add a new Model Version (Select name.xcdatamodeld then Editor->Add model Version) before making any changes, if you have an app already submitted to App Store which is using the earlier model version.

Then, Add a new file from Core Data Tab, as Mapping Model Select, Source Model (Model Version which the submitted App is using) Destination Model (Model Version in which you have done the Changes)

source

But my data mostly are images and app crash because it takes a lot of memory. So I want to delete old data model and its datas and create empty new model data when user update their app. How to do this?

解决方案

If the data model changes you can simply check what model the database file has. If it is not the new one, delete the file specified in the StoreCoordinator with NSFileManager and init your StoreCoordinater and NSManagedContext again to create a new one.

Something like that (not tested code):

var error: NSError
var applicationDocumentsDirectory: NSURL = NSFileManager.defaultManager().URLsForDirectory(NSDocumentDirectory, inDomains:NSUserDomainMask).lastObject
let storeURL: NSURL = applicationDocumentsDirectory.URLByAppendingPathComponent("Database.sqlite")
NSFileManager.defaultManager().removeItemAtPath(storeURL.path, error)


Update for Swift 4:

    do
    {
        var applicationDocumentsDirectory: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!
        let storeURL: URL = applicationDocumentsDirectory.appendingPathComponent("Database.sqlite")
        try FileManager.default.removeItem(atPath: storeURL.path)
    }
    catch
    {
        print(error.localizedDescription)
    }


If the model did not change, you need to save the information of the update anywhere. A text file, in the database itself or in UserDefaults. You just need a flag to check, whether the database has been updated/cleaned.

You can then also delete the database like above or fetch all objects and delete them.

这篇关于如何简单地删除旧的核心数据并重建新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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