Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Previewing ContentView with CoreData

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 access the coredata stack. This works fine when building to a simulator but not with a PreviewProvider

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var managedObjectContext

    @FetchRequest(entity: ProgrammingLanguage.entity(), sortDescriptors: [
            NSSortDescriptor(keyPath: \ProgrammingLanguage.name, ascending: true),
            NSSortDescriptor(keyPath: \ProgrammingLanguage.creator, ascending: false)
        ]) var languages: FetchedResults<ProgrammingLanguage>

    var body: some View {
        NavigationView {
            List {
                ForEach(languages, id: \.self) { language in
                    Text("Language: \(language.name ?? "Anonymous")")
                }
            }
            .navigationBarTitle("My Languages")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

When I try to pass in argument to the ContentView in ContentView_Previews like so I get the following compiler error.

ContentView(managedObjectContext: managedObjectContext)

Error: Instance member 'managedObjectContext' cannot be used on type 'ContentView_Previews'

Maybe this isn't supported by SwiftUI previews yet? Or is there anything that could fix this?

I'm running Xcode 11 Beta 7.

like image 485
keydogg Avatar asked Aug 28 '19 21:08

keydogg


People also ask

Is there a preview Template that uses Core Data?

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!

Can a SwiftUI contentview preview access the coredata stack?

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 access the coredata stack. This works fine when building to a simulator but not with a PreviewProvider

Can preview access the coredata stack from the @environment?

Wondering what the correct way to setup the @environment so the preview can access the coredata stack. This works fine when building to a simulator but not with a PreviewProvider

What is contentview in Maui?

The .NET Multi-platform App UI (.NET MAUI) ContentView is a control that enables the creation of custom, reusable controls. The ContentView class defines a Content property, of type View, which represents the content of the ContentView.


1 Answers

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)

like image 144
bjnortier Avatar answered Sep 19 '22 18:09

bjnortier