xcode 删除 OS X 上的 CoreData 存储?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5954541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:00:50  来源:igfitidea点击:

Deleting CoreData store on OS X?

xcodemacoscore-datamax

提问by shadowhorst

I'm playing around with CoreData in a Mac OS X application. After changing an entity i got the following error:

我正在 Mac OS X 应用程序中使用 CoreData。更改实体后,我收到以下错误:

The managed object model version used to open the persistent store is incompatible with the one that was used to create the persistent store.

用于打开持久存储的托管对象模型版本与用于创建持久存储的版本不兼容。

All answers i've found for this issue suggest implementing versioning/migration into the app, but I'm not interested in saving my data. Isn't there a less complicated solution for that? Like deleting the stock file or something like that? And if yes, where is this file located?

我为这个问题找到的所有答案都建议在应用程序中实现版本控制/迁移,但我对保存我的数据不感兴趣。难道没有更简单的解决方案吗?像删除股票文件或类似的东西?如果是,此文件位于何处?

Thanks.

谢谢。

回答by ughoavgfhw

If you don't want the data, then yes, you can simply delete the old file and create a new one. If your data is document based, then the document itself should be deleted. If you use a single store for the whole application (not document based), then you should look in the code that creates the store object to find the location. The template places the creation code in the application delegate, and the default location for the store is in ${HOME}/Library/Application Support/${APP_NAME}/.

如果您不想要数据,那么是的,您可以简单地删除旧文件并创建一个新文件。如果您的数据是基于文档的,则应删除文档本身。如果您对整个应用程序使用单个存储(不是基于文档的),那么您应该查看创建存储对象的代码以查找位置。模板将创建代码放在应用程序委托中,存储的默认位置在${HOME}/Library/Application Support/${APP_NAME}/.

回答by Andrew

On OS X 10.7 Lion when the app is sandboxed, it is stored in:

在 OS X 10.7 Lion 上,当应用程序被沙盒化时,它存储在:

~/Library/Containers/com.yourcompany.yourAppName/

回答by Andy

I found that for Mac OS X 10.8 / Xcode 4.6 the data is stored in the derived data folder of under

我发现对于 Mac OS X 10.8 / Xcode 4.6,数据存储在

Users/*username*/Library/Developer/Xcode/DerivedData/*app name*-*random string*/Build/Products/Debug/*app name*.sqlite

The easiest way to delete this data is to go to the organiser, select project tab and click "Delete..." button by derived data.

删除此数据的最简单方法是转到组织者,选择项目选项卡,然后按派生数据单击“删除...”按钮。

回答by jiminybob99

Alternatively, For OSX, use the Nsfilemanager to delete the file by using the url defined in the lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator{...}

或者,对于 OSX,使用 Nsfilemanager 通过使用中定义的 url 删除文件 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator{...}

In this var persistentStoreCoordinator a url will be defined that is designed to be used by the persistentStoreCoordinator. You can just use the url defined there to delete the old store. Insert the below code. Run the project and the file will be deleted. Then delete the code to allow objects to be stored againand not delete the data every time.

在这个 var persistentStoreCoordinator 中,一个 url 将被定义为被persistentStoreCoordinator 使用。您可以只使用那里定义的 url 来删除旧商店。插入下面的代码。运行项目,文件将被删除。然后删除代码,允许对象再次存储,而不是每次都删除数据。

here is what I found in the lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator

这是我在 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator

let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("CocoaAppCD.storedata")

here is what i put immediately underneath it.

这是我直接放在它下面的东西。

do{
    try NSFileManager.defaultManager().removeItemAtURL(url)
} catch{
    print("could not delete file")
}

then i clicked run. It ran once then i delete the code i just made so that the data would not be deleted every time

然后我点击运行。它运行了一次然后我删除了我刚刚制作的代码,这样数据就不会每次都被删除

回答by Ivan Yurchenko

Try to run the app in a simulator of another devicethat you haven't used before and see if it helps you (i.e. iPhone SEinstead of iPhone 7). If it does, then removing the corrupted folders should help. Open Terminaland run the following commands:

尝试在您以前未使用过的另一台设备的模拟器中运行该应用程序,看看它是否对您有帮助(即iPhone SE而不是 iPhone 7)。如果是这样,那么删除损坏的文件夹应该会有所帮助。打开终端并运行以下命令:

sudo rm -rf ~/Library/Developer/CoreSimulator/Devices

to remove the simulators' data that might be corrupted,

删除可能已损坏的模拟器数据,

sudo rm -rf ~/Library/Developer/XCode/DerivedData/

to remove your app's data.

删除您的应用程序数据。

Hope that helps!

希望有帮助!

回答by Edward Loveall

On Mac OS X 10.11.4 with Xcode 7.3 cleaning the fixed the problem for me:

在 Mac OS X 10.11.4 上使用 Xcode 7.3 清理为我解决了这个问题:

Product> Cleanor Shift?K

Product>CleanShift?K