NSManagedObject properties becoming nil when passed to view from PreviewProvider

I have a view that will display available genres. Genre is a NSManagedObject containing the title of the genre.

I am trying to create a Samples class that provides sample data to be used in the PreviewProvider.

However, the Genre returned from the Samples class has its genre string set to nil.

For the life of me...I cannot figure out why this is happening:

Code Block swift
class Samples {
    static var sampleGenre: Genre {
        let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
        let genre = Genre(context: context)
        genre.genre = "Horror"
        return genre
    }
}


Then in the PreviewProvider:
Code Block swift
struct GenreRow_Previews: PreviewProvider {
    static var previews: some View {
        GenreRow(genre: Samples.sampleGenre)
    }
}


The GenreRow is simple:
Code Block swift
struct GenreRow: View {
   var genre: Genre
    var body: some View {
        HStack {
            Text(genre.genre ?? "Unknown genre")
        }
        .padding()
    }
}


The text is set to "Unknown genre" even though one has previously been set.

Any guidance would be appreciate. How are people previewing views with NSManagedObjects?

NSManagedObject properties becoming nil when passed to view from PreviewProvider
 
 
Q