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

Code before:
        var sectionedResults: SectionedResults<Key, ResultType>!
        var token: AnyCancellable?

        override func updateValue() {
            let realm = try! Realm(configuration: configuration ?? Realm.Configuration.defaultConfiguration)
            var results = realm.objects(ResultType.self)

            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 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

Review comment (suggestion):
```suggestion
             Observing the sectioned results directly doesn't allow the SwiftUI diff to work
```

Code after:
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper public struct ObservedSectionedResults<Key: _Persistable & Hashable, ResultType>: DynamicProperty, BoundCollection where ResultType: _ObservedResultsValue & RealmFetchable & KeypathSortable & Identifiable {
    public typealias Element = ResultType

    private class Storage: ObservableResultsStorage<Results<ResultType>> {
        var sectionedResults: SectionedResults<Key, ResultType>!
        var token: AnyCancellable?

        override func updateValue() {
            let realm = try! Realm(configuration: configuration ?? Realm.Configuration.defaultConfiguration)
            var results = realm.objects(ResultType.self)

            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>,