1

I am currently migrating some SwiftUI code from combine to the new observation framework and I want to know how I can I maintain debounce? Is there anyway to natively debounce from within the Observation framework?

I have tried combining Combine ~no pun intended~ with the Observation framework and it produces a slew of errors.

  • 1
    Can you provide a code sample that demonstrates the "slew of errors"? The Observer mechanism in the SwiftUI framework, and Combine, are not replacements for one another so replacing one with the other may not be appropriate in all cases. – Scott Thompson Aug 12 '23 at 13:55
  • Meaning I wanted an idiomatic way to debounce code when using the Observer macro, right now it appears as if you can only debounce from the old mechanism using combine, did apple include a way to do this from the Observer macro? – Stannis Baratheon Aug 12 '23 at 15:20

2 Answers2

1

No, there is no debounce because the Observer mechanism is designed for the synchronous delivery of information.

Debouncing involves a delay and therefore, by its very nature, is concerned with the asynchronous delivery of information. As such, debounce would be out-of-place in the Observer mechanism.

Combine is for handling asynchronous messages and debounce is appropriate in that system.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
0

debounce is in the AsyncAlgorithms Swift package here:

https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/AsyncAlgorithms.docc/Guides/Debounce.md

In Xcode choose File->Add Package Depdendencies and choose AsyncAlgorithms.

If you were previously using Combine's ObservableObject without an async pipeline then you don't need debounce because SwiftUI already debounces all state changes into one single update event.

malhal
  • 26,330
  • 7
  • 115
  • 133