Repository: realm/realm-swift
File: RealmSwift/SwiftUI.swift (Swift)

Code before:
            }

            if let keyPathString = keyPathString, sortDescriptors.isEmpty {
                sortDescriptors.append(.init(keyPath: keyPathString, ascending: true))
            }

            value = results

            /*
             Observing the sectioned results directly doesnt allow the SwiftUI diff to work
             correctly as the previous state of the sectioned results will have the new values.

             An example of when this is an issue is when an item is deleted in a List containing sectioned results,
             the diff needs a stable state of the previous transaction but due to
             the observation callback calling calculate_sections the collection will be brought up to date.

             The solution around this is to store a frozen copy of the sectioned results and observe the parent `Results` instead.
             Each time the results observation callback is invoked and the SwiftUI View is redrawn the sectioned results will be updated.
             */
            sectionedResults = value.sectioned(sortDescriptors: sortDescriptors, sectionBlock).freeze()
            token = self.objectWillChange.sink { [weak self] _ in
                guard let self = self else { return }
                self.sectionedResults = self.value.sectioned(sortDescriptors: self.sortDescriptors, self.sectionBlock).freeze()
            }
        }

        var sortDescriptors: [SortDescriptor] = [] {
            didSet {
                didSet()
            }
        }
        var sectionBlock: ((ResultType) -> Key)
        var keyPathString: String?

        init(_ value: Results<ResultType>,
             sectionBlock: @escaping ((ResultType) -> Key),
             sortDescriptors: [SortDescriptor],
             keyPathString: String? = nil,
             keyPaths: [String]? = nil) {
            self.sectionBlock = sectionBlock
            self.sortDescriptors = sortDescriptors
            if let keyPathString = keyPathString {
                self.keyPathString = keyPathString
                self.sortDescriptors.append(.init(keyPath: keyPathString, ascending: true))
            }
            if self.sortDescriptors.isEmpty {
                throwRealmException("sortDescriptors must not be empty when sectioning ObservedSectionedResults with `sectionBlock`")
            }
            super.init(value, keyPaths)
        }
    }

Review comment (suggestion):
Do we really need to add an observation here as well and update the sectionedResults value if there is change in value in results, isn't the above code doing it already, it seems like you are duplicating updating `sectionedResults`

Code after:

            let filters = [searchFilter, filter].compactMap { $0 }
            if !filters.isEmpty {
                let compoundFilter = NSCompoundPredicate(andPredicateWithSubpredicates: filters)
                results = results.filter(compoundFilter)
            }

            if let keyPathString = keyPathString, sortDescriptors.isEmpty {
                sortDescriptors.append(.init(keyPath: keyPathString, ascending: true))
            }

            value = results

            /*
             Observing the sectioned results directly doesn't allow the SwiftUI diff to work
             correctly as the previous state of the sectioned results will have the new values.

             An example of when this is an issue is when an item is deleted in a List containing sectioned results,
             the diff needs a stable state of the previous transaction but due to
             the observation callback calling calculate_sections the collection will be brought up to date.

             The solution around this is to store a frozen copy of the sectioned results and observe the parent `Results` instead.
             Each time the results observation callback is invoked and the SwiftUI View is redrawn the sectioned results will be updated.
             */
            sectionedResults = value.sectioned(sortDescriptors: sortDescriptors, sectionBlock).freeze()
            token = self.objectWillChange.sink { [weak self] _ in
                guard let self = self else { return }
                self.sectionedResults = self.value.sectioned(sortDescriptors: self.sortDescriptors, self.sectionBlock).freeze()
            }
        }

        var sortDescriptors: [SortDescriptor] = [] {
            didSet {
                didSet()
            }
        }
        var sectionBlock: ((ResultType) -> Key)
        var keyPathString: String?

        init(_ value: Results<ResultType>,
             sectionBlock: @escaping ((ResultType) -> Key),
             sortDescriptors: [SortDescriptor],
             keyPathString: String? = nil,
             keyPaths: [String]? = nil) {
            self.sectionBlock = sectionBlock
            self.sortDescriptors = sortDescriptors
            if let keyPathString = keyPathString {
                self.keyPathString = keyPathString
                self.sortDescriptors.append(.init(keyPath: keyPathString, ascending: true))
            }
            if self.sortDescriptors.isEmpty {