Using Core Data in a new to macOS 12 (beta) `Table`

My project is a universal app with iOS 15 and macOS 12 targets, to attempt to take advantage of latest technologies, including SwiftUI and Table.

I let Xcode take care of the CodeGen by selecting type "Class Definition" and then write Extensions for custom properties.

I have an entity named Event with a class name PTG_Event. Each Event can have a number of actions and so each Event also has a one-to-many relationship with Action (class PTG_Action).

I am attempting to use Table to display the actions for each event... but this doesn't work...

struct ActionTable: View {
    @ObservedObject var event: PTG_Event

    @FetchRequest(entity: PTG_Action.entity(),
                  sortDescriptors: []
    ) var eventActions: FetchedResults<PTG_Action>

    @State var sortOrder: [SortDescriptor<PTG_Action>] = [
        .init(\.dateCommences, order: SortOrder.forward)
    ]

    var body: some View {
        let events = eventActions.filter { $0.event == event }
        Text("Action Table")
        Table(events, sortOrder: $sortOrder) {
            TableColumn("Ref", value: \.reference)
            { action in
                Text(action.reference ?? "NO REF")
            }
            TableColumn("Date", value: \.dateCommences)
            { action in
                Text(action.dateCommences?.formatted(date: .abbreviated, time: .omitted) ?? "NO DATE")
            }
        }
    }
}

I have attempted many variations on this.

Any suggestions?

Try this:

   Table(selection: $selectedAction, sortOrder: $sortOrder) {
            TableColumn("Ref", value: \.reference) { action in
                Text(action.reference ?? "NO REF")
             }
            TableColumn("Date", value: \.dateCommences) { action in
                Text(action.dateCommences?.formatted(date: .abbreviated, time: .omitted) ?? "NO DATE")
             }
      } rows: {
            ForEach(eventActions) { action in
                TableRow(action)
           }
      }
   }

Ironically it’s not the code for Table() that was the issue.

Table is itself a ScrollView

At the time of writing this, Table does not function when it is embedded into another ScrollView.

So, all I needed to make Table structure function as expected, is to remove it from the ScrollView!

Many thanks to Apple DTS for solving this.

Using Core Data in a new to macOS 12 (beta) `Table`
 
 
Q