CoreData inside of Actor

Im seeing some crashes due to concurrent accesses to CoreData, even though its inside of an Actor. Is there any issue with CoreData using the Actor? (i know actor uses a queue like com.apple.root.user-initiated-qos.cooperative, which does not sound like serial queue, so Im just wondering if theres problems when non thread safe code such as CoreData runs in the same queue.)

I could think of three pieces of advice to start:

  1. Keep the com.apple.CoreData.ConcurrencyDebug flag active for your debug builds.[1] This can help turn inconsistent crashes into deterministic crashes.
  2. Construct the NSManagedObjectContext isolated to your actor. If your actor uses a nonisolated init that constructs your NSManagedObjectContext this would construct on the thread that created your actor.
  3. Also ask the question on Apple Developer Forums. This forum is mostly for questions related to the open source Swift tech stack. Not so much for questions about trying to debug the closed source frameworks from Apple.

  1. Debugging Core Data ↩ī¸Ž

2 Likes

Also, have a look at what the ModelActor macro generates. It's only directly usable for SwiftData, but probably answers your questions.

1 Like