Learn Courses My Dashboard

CoreData and PreviewProvider

Can anyone advise on how to create a CoreData class instance that can be used in PreviewProvider.

Thank you

I have solved this issue incase anyone is interested.

Consider having a CoreData object called Thread which has 2 attributes; threadText and dateSent.

A single object of this type is to be displayed in a view called ThreadRow and the object instance is to be passed in as an argument called thread.

The PreviewProvider would look like this:

struct ThreadRow_Previews: PreviewProvider {

static var viewContext = PersistenceController.preview.container.viewContext

static var previews: some View {
    let thread = CDThread(context: viewContext)
    thread.threadText = "Hello"
    thread.dateSent = Date()
    return ThreadRow(thread: thread)
                .environment(\.managedObjectContext, viewContext)