Karhin’s Blog
Apps, Design and Music

Starting in iOS 17, Apple introduced a new framework called Observation. It does make life a bit easier in the context of SwiftUI, but integration with UIKit (or simply the task of wiring together different Observable objects) can cause some problems, since the only tool available is withObservationTracker.

If you are going to build an app for iOS 26 or later, take a look at Observations; you do not need to read the rest of the article. As an alternative, you can take a look at Perception, it may be a good option.

With it, almost everything is fine except for one issue: it assumes a recursive call to itself, and there is no way to cancel it in the form in which it is proposed to be used.

I tested the following code on the iOS 17 simulator and on current operating systems, including macOS, and it works like a charm (yes, it’s as simple as it looks). If you have any comments, you are always welcome to share them.

@MainActor
final class ObservationTracker {
    
    private let apply: () -> Void
    
    init(_ apply: @escaping () -> Void) {
        self.apply = apply
        observe()
    }
    
    private func observe() {
        withObservationTracking({ [weak self] in
            self?.apply()
        }, onChange: { [weak self] in
            Task { @MainActor in
                self?.observe()
            }
        })
    }
    
}

If you remove the reference to the object, observation will stop immediately (you can verify this in the debugger). Most importantly, do not keep strong references inside the callback. This can result in a memory leak, but that generally applies to withObservationTracker as well.

var observer: ObservationTracker?

...

observer = ObservationTracker { [weak self] in
    self?.time = self?.timeModel.time
}

If you need a more classic syntax with cancel, you can add some kind of flag to the onChange method that will stop the recursive call. In general, it was enough for me to simply set the object to nil.

633 Digital Swift Programming SwiftUI UIKit

To post a comment, please log in or create an account.

Sign In

Первый iPhone

Возле первого айфона часто вижу приписки «Революционный», «Прорывной» и так далее. У меня совсем другое мнение по этому поводу.

Как сделать сайт немного лучше

Есть один очень простой способ, как сделать сайт чуть-чуть лучше, который знает практически любой разработчик мобильных или десктоп приложений, но который не знают веб-разработчики.

Пять последних функций iOS, о которых вы не знали

Собрал пять моих любимых фичей, которые делают работу с iOS намного приятнее. Некоторые из этих функций вскользь упоминались на презентациях, некоторые выпускали с неосновными обновлениями операционной системы и про них уже все забыли.

Git за пять минут

Если вы всегда хотели разобраться с гитом и узнать, что это такое и как с ним работать, то сейчас самый подходящий момент.