How to use withObservationTracking to monitor an @Observable

Published by malhal on

Reposted from: https://forums.swift.org/t/how-to-use-observation-to-actually-observe-changes-to-a-property/67591/18

I thought I’d share an example of how I am currently using it. I wanted to make an object that maintains a sorted order of the main model’s objects. I used withObservationTracking to update if either the sort order local to this object is changed or if the particular model object property I’m sorting by is changed. It doesn’t matter that tracking only happens once, because if anything I’m tracking changes, the cached data will be invalidated and when it is requested again the tracking will be re-configured.

@Observable
class Counter: Identifiable {
    var count = 0
}

@Observable
class Model {
    var counters: [Counter] = [.init(), .init(), .init()]
}

@Observable
class SortedModel {
    @ObservationIgnored
    var model: Model! {
        didSet {
            if oldValue !== model {
                _counters = nil
            }
        }
    }

    var ascending = false
    
    var _counters: [Counter]?
    var counters: [Counter] {
        if _counters == nil {
            _counters = withObservationTracking {
                // tracks self.ascending and the count of all model.counters.
                let sort = SortDescriptor(\Counter.count, order: ascending ? .forward : .reverse)
                return model.counters.sorted(using: sort)
            } onChange: { [weak self] in
                self?._counters = nil
            }
        }
        return _counters!
    }
}
Categories: SwiftSwiftUI

نيك قحاب arabianmotion.com سكس ليلةالدخلة hot indian girl nude fuckvidstube.com 3gp porn movies 全裸婚活パーティー javsearch.mobi サカリ 葉月桃 xxx heroine movsmo.net telugu heroines sex سكس ميا porn-planet.org سكسى نيك
hot natalie martinez tubemania.org xvideks افلام كلاسيك مترجمه esarabe.com ادوج فينش watch mygf pornmd.pro gujarati indian sex indian high profile sex manytubeporn.mobi downloadhub.link سكس فى الغابه realarabporn.com سكس المصيف
افلام جنس قصيرة arabianreps.com جسمها نار romance x indianpornvideos.me bada dudha 無修正 マジックミラー javmovie.pro 鬱勃起 افضل مواقع السكس المصري pornwap.tv سكس بيوت الدعاره سكس مصريhd arab-porn.org كس متحرك