Deleting CoreData store on OS X?

2019-03-12 20:28发布

问题:

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

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.

回答1:

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}/.



回答2:

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

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


回答3:

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

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.



回答4:

Alternatively, For OSX, use the Nsfilemanager to delete the file by using the url defined in the 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 again and not delete the data every time.

here is what I found in the 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



回答5:

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

Product > Clean or ShiftK



回答6:

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

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!