SwiftUI, Firebase, and Table

Now that I can build again, I'm back to experimenting, this time using Firebase. To display my items, I set up a Table, and this mostly works, barring a couple of minor, crashy details.

I've got

    @State var selectedItems = Set<Item.ID>()
    private var tableData: [Item] {
        return self.items.sorted(using: self.sortOrder)
    }

    var body: some View {
        Table(self.tableData, selection: self.$selectedItems, sortOrder: self.$sortOrder) {
            TableColumn("Item", value: \.name)
            TableColumn("Count", value: \.count) { item in
                Text("\(item.count)")
            }
    }

Edited down a bit but that's the basics.

The two problems I've got are:

  • When I select a row, it flashes but does not stay highlighted
  • If I select the same row again, it crashes with:
Fatal error: Duplicate elements of type 'Optional<String>' were found in a Set.
This usually means either that the type violates Hashable's requirements, or
that members of such a set were mutated after insertion.

I put in a willSet for the selectedItems which was not particularly helpful.

This doesn't happen with the CoreData version, so I assume it's something wonky about Firebase. Or my limited skills.

Searching for this crash doesn't seem to show anything useful.

Answered by kithrup in 772429022

In retrospect possibly obvious: the Firebase id is optional, so I added a slight layer of abstraction and use a type that has a non-optional id element. (I'd already had this type for other reasons, so it was a minor refactor.) With that done, it worked as expected. Including not crashing, which is always a nice benefit!

Accepted Answer

In retrospect possibly obvious: the Firebase id is optional, so I added a slight layer of abstraction and use a type that has a non-optional id element. (I'd already had this type for other reasons, so it was a minor refactor.) With that done, it worked as expected. Including not crashing, which is always a nice benefit!

SwiftUI, Firebase, and Table
 
 
Q