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?

Replies

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)
           }
      }
   }
  • Thank you for your answer, greatly appreciated. I have attempted to use this solution. The project builds and runs, but unfortunately still no show for the Table. Switching between records, the two column Table flashes up for only a few microseconds (which it also does for the code in the original post).

    Just to double check my data is coming through, I added the following...

    Text("\(eventActions.count) ACTIONS")

    For an event with 3 actions, this accurately reports this. Perhaps worth mentioning that I have built the GardenApp code-along tutorial projects and these function as expected. So I suspect that this is either an error in my code structure or a bug (which I have already reported in feedback to Apple).

Add a Comment

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.