Xcode bug in SwiftUI previews when using Core Data

Hi, guys. Can someone confirm this problem so I submit the bug to Apple. When the Core Data Persistent Store is empty, the canvas preview crashes. To test it, you just need to create a new project with the Core Data option enabled. Then go to the Persistence.swift file created by the template, and remove the for in loop in the closure assigned to the preview property that creates 10 new items. The property will look like this:

static var preview: PersistenceController = {
   let result = PersistenceController(inMemory: true)
   return result
}()

Now, you will get an error when the system tries to fetch items [NSManagedObjectContext executeFetchRequest:error:].

You only need to add one single item for the error to disappear. It looks like a pretty bad bug, so I want to be sure it is not my computer.

Thanks.

  • Yeah that's dirty little bug in the template. Not sure what its creator was thinking.

  • Do you know a better way to create the persistent store for the previews? I tried other approaches but the previews always crashes.

Add a Comment

Accepted Reply

I solved all the issues by replacing the NSSortDescriptor class by the SortDescriptor structure.

Replies

I solved all the issues by replacing the NSSortDescriptor class by the SortDescriptor structure.