SwiftData randomly throwing EXC_BAD_ACCESS on Model entity creation and update

There is something that I'm spending hours trying to solve but start to be blocked. On one of my app projects, I'm experiencing random fatal errors during the access and update of SwiftData Models, which does not occur when using CoreData.

This mainly happens when setting value for newly created entities: SwiftData randomly crashes the application with the following error: EXC_BAD_ACCESS (code=1, address=0x11). I made sure to update the code to move operations in the background and to use a ModelActor. It works properly, but 1 out of 3 times, it crashes with the error quoted.

I also tried to look at previous posts and made sure to persist the context when creating new entities, before updating them.

The last information in my console before “(lldb)” is:

CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate managedObjectContextSaved:](2996): <NSCloudKitMirroringDelegate: 0x2806342a0>: Observed context save: <NSPersistentStoreCoordinator: 0x281434c40> - <NSManagedObjectContext: 0x280450820>
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate remoteStoreDidChange:](3039): <NSCloudKitMirroringDelegate: 0x2806342a0>: Observed remote store notification: <NSPersistentStoreCoordinator: 0x281434c40> - FF2D0015-7121-4C30-9EE3-2A51A76C303B - <NSPersistentHistoryToken - {
    "FF2D0015-7121-4C30-9EE3-2A51A76C303B" = 1316;
}> - file:///private/var/mobile/Containers/Shared/AppGroup/ED0B229A-F5BC-47B7-B7BC-88EEFB6E6CA8/Library/Application%20Support/default.store
CoreData: debug: CoreData+CloudKit: -[NSCloudKitMirroringDelegate managedObjectContextSaved:](2996): <NSCloudKitMirroringDelegate: 0x2806342a0>: Observed context save: <NSPersistentStoreCoordinator: 0x281434c40> - <NSManagedObjectContext: 0x280450820>.

In the rare occasion where it provides more details, Xcode shows issue in the @Model macro, as shown in the screenshot attached. I was able to reproduce this issue on 4 different physical devices and in the simulator.

It also happens, less frequently, when trying to fetch a Model entity from the SwiftData context. Not for specific models tho, it can happen for all of them, randomly.

I was able to experience this random problem both on iOS 17.0 Developer Beta, RC & the official iOS 17.0 release. At the time in the beta Xcode 15, and now the latest stable Xcode.

Am I the only one having this kind of issues? Or is it a known issue with Swift? Anybody has idea how I could solve that?

Is the ModelActor working for you? I've noticed that no matter what it's always on the main thread. Other developers are seeing this too in Xcode 15 b8 and above.

Good question @tundsdev! I did not check before, but I just did it and yes, the code in each ModelActor is always running in the main thread. I'm quite surprised!

In the same situation, adding @MainActor + async to all methods of my SwiftDataService helped me. For example: @MainActor func updatePerson(person: Person) async { ... }

Unfortunately, it doesn't look like multithreading :/

Have you found an answer to this? My code is currently creating SwiftData entities each on a background thread and sometimes it works (73 created) and sometimes it crashes on a random one. My thread sidebar looks the same for the crashed thread.

I had a similar issue when adding entities on a background thread. Got it working by working in a different context. So rather than passing in the entity I passed in the model container and entity persistent id and recreated in in a new context. Share more about it here: https://www.simplykyra.com/swiftdata-solving-fatal-errors-and-exc_bad_access-while-handling-entities-on-different-threads/

SwiftData randomly throwing EXC_BAD_ACCESS on Model entity creation and update
 
 
Q