SwiftUI previews and CloudKit sharing

The SwiftUI templates provided by Xcode typically create an in-memory store for preview purposes. I started from one of these templates and added the necessary code for working with CloudKit shares, but now the existing preview store creation gets runtime errors I'm struggling to understand.

The ultimate error is:

FAULT: NSInternalInconsistencyException: Unsupported feature in this configuration; {
    store = "<NSSQLCore: 0x12e26b170> (URL: file:///dev/null)";
}

There are other things in the log like:

warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Trip' so +entity is unable to disambiguate.

This might be due in part to the normal full stack being instantiated during a unit test, but that was the only way I could step through the code to try to see what was causing the SwiftUI preview to crash.

I can't build the full core data stack as normal, because then the unit tests and previews pollute the real store.

Answered by JetForMe in 811218022

After a lot of tinkering, I came upon a solution. Not sure if all of this is necessary:

  1. Only initialize the full stack if not in a SwiftUI preview. This was *****, relying on a runtime environment variable.
  2. Create a simple NSPersistentContainer when using in-memory store for previews and unit tests
  3. Only call setQueryGenerationFrom(.current) for the "real" stack, not the preview stack. This last one was key.
Accepted Answer

After a lot of tinkering, I came upon a solution. Not sure if all of this is necessary:

  1. Only initialize the full stack if not in a SwiftUI preview. This was *****, relying on a runtime environment variable.
  2. Create a simple NSPersistentContainer when using in-memory store for previews and unit tests
  3. Only call setQueryGenerationFrom(.current) for the "real" stack, not the preview stack. This last one was key.
SwiftUI previews and CloudKit sharing
 
 
Q