Previewing ContentView with CoreData

后端 未结 3 665
长情又很酷
长情又很酷 2020-12-05 08:16

When I try to a SwiftUI ContentView that contains a CoreData fetch request, the preview crashes. Wondering what the correct way to setup the @environment so the preview can

3条回答
  •  天命终不由人
    2020-12-05 08:48

    Credit goes to @ShadowDES - in the Master/Detail template project in Xcode Beta 7 there's a preview that uses Core Data:

    #if DEBUG
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
            return ContentView().environment(\.managedObjectContext, context)
        }
    }
    #endif
    

    This works for me in Xcode Beta 5 (my Beta 7 crashes)

提交回复
热议问题