I have a Firestore listener that listens for any changes to a summary collection/document.
if summaryListenerRegistration == nil {
summaryListenerRegistration = db.collection("partner").document(userUID).collection("summary").document("summary").addSnapshotListener { documentSnapshot, error in
I need to present information from this document (nested map info) in a sorted list in my main View. My question is this – where should I be sorting? Should it be in the actual listener or on the list in the view? I am currently sorting in the listener with
self.summaryItem.sort()
which uses the a static function that I already have imbedded in my SummaryItem structure.
struct SummaryItem: Codable, Identifiable, Hashable, Comparable {
static func <(lhs: SummaryItem, rhs: SummaryItem) -> Bool {
return lhs.name < rhs.name
}
var id: UUID = UUID()
var name: String
var fileName: String
var author: String
var category: String
}
My main view has the following for presenting the “list” (without sorted {$0.name , $1.name} included):
ForEach(model.summaryItem) { summary in
BookView(book: summary)
.tag(summary)
}
Edit – Depending on which way this is handled, in the Listener or in the ForEach, the results on the UI are different. I’m not sure why that’s the case since trying this the other way
ForEach(model.summaryItem.sorted {$0.name , $1.name}) { book in
is referenced by a @Published variable. So, it doesn’t really work the way I would expect.
7