How to completely reset CoreData for an Xcode project?
Asked Answered
E

3

5

My macOS application is a mess right now. I made some big changes to my Core Data model, and now, just launching the app causes the app and Xcode to crash. The app's crash log reveals the following reason for the crash:

Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=134140 "Persistent store migration failed, missing mapping model."

I don't want to create a mapping model. I don't care about the old CoreData model. I want all that data to go away. I have tried the following things:

  1. Deleting Derived Data.
  2. Deleting the ~/Library/Application Support/APP_NAME/ folder.
  3. Using NSBatchDeleteRequest to delete all the entities.

I want to start fresh (without creating a completely new project). What do I need to do to start all over again with a fresh Core Data dataset ?

Enamelware answered 9/2, 2018 at 2:44 Comment(9)
is your app live ?Monreal
@Dev_Tandel no.Enamelware
Then you can always delete app from your device and start as newMonreal
If I’m making a macOS app, then doesn’t deleting Derived Data already do that? That’s where the executable is stored right?Enamelware
If you make changes in code data then you must do DBMigration in case of the app is live else you can just delete the app from your device/simulator and start as fresh.Monreal
derived data and app installed is both different things. Derived data is a folder that contains build info, indexing and all not the real app.Monreal
@Willeke Isn’t it stored in the Application Support directory? I’ve already deleted my app’s folder in that.Enamelware
Search in the Finder for yourAppName.sqlite. I found one in ~/Library/Containers/yourcompany.yourAppName/Data/Library/Application Support/yourAppName/.Anisette
@Anisette That worked. Put your comment as an answer so I can accept it.Enamelware
A
8

Search in the Finder for yourAppName.sqlite. I found one in

~/Library/Containers/yourcompany.yourAppName/Data/Library/Application Support/yourAppName/
Anisette answered 9/2, 2018 at 13:37 Comment(0)
B
2

Well if you are using the emulator, simply delete all data on it like so:

Select Simulator -> Device -> Erase All Content and Settings...

Click erase button

Breen answered 8/12, 2022 at 17:16 Comment(0)
I
1

Well, like @Willeke said, it seems to be as simple as finding the sqlite file and deleting it. That gets you a complete purge/reset without rebuilding the app. Still a pain though if you just want to tweak the structure a bit without losing the accumulated data. So for that scenario, maybe develop an export function first, dump the data, delete the sqlite file, then re-import the data.

Importance answered 19/10, 2021 at 0:4 Comment(2)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewBirdhouse
I am not sure why. I was getting exactly the same error message and was searching for this very answer. I just confirmed that deleting it completely resets the core data model. It did totally cure my problem. I had made significant changes to the entities, and was crashing for exactly the same reason as the OP, and wanted to reset, just like the OP. @Anisette identified the right thing to delete. I just confirmed it. I did add the extra bit about preserving the original data if desired, but that was just an optional extra. Perhaps I should have skipped that part of the comment? I'm confused.Importance

© 2022 - 2024 — McMap. All rights reserved.