Using CoreData entity in SwiftUI view

Hello, I'm a little confused as to how to use my attributes that are in an entity in CoreData in a SwiftUI View. In this case, I have an entity called "Meeting", and it has string attributes called "meetingName" and "meetingLink". I have set up a persistence controller and everything seems to work fine, but I've run into an issue where I want to display the meetingName attributes in a list. I'm doing this by creating a view called MeetingRow, then using the following HStack:

Code Block
struct MeetingRow: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(
        entity: Meeting.entity(),
        sortDescriptors: [
            NSSortDescriptor(keyPath: \Meeting.meetingName, ascending: true),
            NSSortDescriptor(keyPath: \Meeting.meetingLink, ascending: false)
        ]
    ) var meetings: FetchedResults<Meeting>
    var body: some View {
        HStack {
            Spacer()
            .frame(width: 20)
            Text(meetings.meetingName ?? "nil")
            }
/* This ^^ is the offending line (error: Value of type 'FetchedResults<Meeting>' has no member 'meetingName' */
            Spacer()
        }
    }


It tells me that I don't have member meetingName in the fetched results, which is why I'm confused. Thank you in advance for your help!
Answered by szymczyk in 661838022
If you removed the meetings property, that error message is stale.

You may need to supply a Meeting object to the preview. One of the following two methods should work:

First, supply an empty Meeting object.

Code Block
MeetingRow(meeting: Meeting())

Second, use the .constant modifier when supplying the Meeting object.

Code Block
MeetingRow(meeting: .constant(Meeting()))

You may need to supply a meeting name, URL, and date to the initializer.

You can also temporarily comment out the preview code to get the app to build.

You created the MeetingRow view to display one meeting, but instead of having a property for the one meeting to display, you have a @FetchRequest property that contains a set of meetings. A meeting has a meetingName property, but a set of meetings does not. The set contains meetings so you get a compiler error.

To fix the error, get rid of the meetings property and add a meeting property of type Meeting. Your Text item will look like the following:

Code Block
Text(meeting.meetingName ?? "nil")

Thank you, so now I have something like this

Code Block
var meeting: Meeting

But the PreviewProvider now says it is missing an argument for the parameter 'meetings'. How do I initialize some dummy data for the preview if the data is in CoreData?

I have 'meetingName' of type string, 'meetingLink' of type URI, and 'meetingTime' of type Date.

Thank you!
Accepted Answer
If you removed the meetings property, that error message is stale.

You may need to supply a Meeting object to the preview. One of the following two methods should work:

First, supply an empty Meeting object.

Code Block
MeetingRow(meeting: Meeting())

Second, use the .constant modifier when supplying the Meeting object.

Code Block
MeetingRow(meeting: .constant(Meeting()))

You may need to supply a meeting name, URL, and date to the initializer.

You can also temporarily comment out the preview code to get the app to build.

Using CoreData entity in SwiftUI view
 
 
Q