-12

I've been working on a small, SwiftUI/SwiftData app for personal usage. This has been going well until yesterday when I decided I should refactor the SwiftData side of things (split some tables up, move things around. All to better map the schema to the application domain). But now the App crashes when it is opened in the simulator, with lots and lots of "CoreData" errors in the log:

CoreData: error: Failed to stat path '/Users/xxxxxx/Library/Developer/CoreSimulator/Devices/272B947E-AC7B-495F-9190-7A9E6113277F/data/Containers/Data/Application/2CA83FBE-E2FC-42C5-878B-B86E751FA667/Library/Application Support/default.store', errno 2 / No such file or directory.
CoreData: error: Failed to stat path '/Users/xxxxxx/Library/Developer/CoreSimulator/Devices/272B947E-AC7B-495F-9190-7A9E6113277F/data/Containers/Data/Application/2CA83FBE-E2FC-42C5-878B-B86E751FA667/Library/Application Support/default.store', errno 2 / No such file or directory.
CoreData: error: Executing as effective user 502
CoreData: error: Executing as effective user 502
CoreData: error: Failed to statfs file; errno 2 / No such file or directory.
CoreData: error: Failed to statfs file; errno 2 / No such file or directory.
CoreData: error: Logging status information for directory path: /Users/xxxxxx/Library/Developer/CoreSimulator/Devices/272B947E-AC7B-495F-9190-7A9E6113277F/data/Containers/Data/Application/261084F1-C9EE-4338-9D89-F2B5FAFDDBC6/Library/Application Support
CoreData: error: Executing as effective user 502
CoreData: error: Executing as effective user 502
CoreData: error: Information for /
CoreData: error: Information for /
CoreData: error: File Device ID: 0
CoreData: error: File Device ID: 0
CoreData: error: Device ID: 16777230
CoreData: error: Device ID: 16777230
CoreData: error: File Size: 704 bytes
CoreData: error: File Size: 704 bytes
...

Indeed, the default.store file (and its parent directory) do not exist.

In setting breakpoints and stepping over code, the crash occurs when setting the ".modelContainer(for:)" in the @main application code.

From googling around, my best guess is that there is a mismatch between my new schema and the schema in the previously created sqlite files that are in the simulator directories.

But I have tried following every suggestion I could find that hinted at how to fix this (deleting the App from the simulator, resetting the simulator etc). Obviously I've failed at that. I think at I should be diving deep into the simulator directory structure and purging the data there. But I am not sure where I should be looking, or what files/directories I should be purging.

How can I recover from this situation?

9
  • 3
    Your crash may not be where you think it originates. Show a minimal reproducible code that produces your issue, see: minimal code. In particular the important model container declaration code. Commented Nov 13 at 22:52
  • Do you get any other errors or log messages relating to this? If SwiftData doesn’t find the database file it shouldn’t crash but rather try to create a new one. Commented Nov 14 at 5:47
  • 1
    My first thoughts were that you may have a typo in your schema or you @Model class, that is not easy for you to see. You could try deleting the sql files, add ".onAppear { let appSupportDir = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).last print("---> (appSupportDir)") }" to your App declaration, and remove the 3 .sqlite files. Commented Nov 14 at 22:57
  • 1
    You said you had reset the simulator etc so the old db file isn’t relevant since it has been deleted, right? This is why I am wondering if there’s any logging related to SwiftData trying to create a new db file. Commented Nov 15 at 8:27
  • 2
    This question has relevant discussion on meta. Since the question here is not asking how or why this happened, but only about how to recover from the situation, code that could show how/why this came to be is not necessary for the question to exist in an open state. Commented Nov 24 at 22:58

1 Answer 1

-1

The answer was staring me in the face from the debug info that I provided. I just failed to recognize it. For whatever reason the iOS simulation system failed to create this directory:

/Users/xxxxxx/Library/Developer/CoreSimulator/Devices/272B947E-AC7B-495F-9190-7A9E6113277F/data/Containers/Data/Application/2CA83FBE-E2FC-42C5-878B-B86E751FA667/Library/Application Support/

This was reported in the multiple "CoreData: error: Failed to stat path" log entries.

I saw this when I navigated to the parent directory. After manually creating the "Application Support" directory with a simple mkdir command, the iOS simulator ran with no error (and with no additional editing of my user facing code).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.