Hey team, the current Swift implementation of obje...
# sdk-native
b
Hey team, the current Swift implementation of objects such as
Participant
as
ObservableObject
with computed properties probably works great for SwiftUI if you bind directly to the objects and properties as the source of truth. But if you want to build code-based, custom data flows it takes a chunk more work. Any plans to migrate to a more reactive-friendly pattern / embracing
@Published
,
CurrentValueSubject,
AsyncSequence
or
@Observable
?
👀 1
b
I think @able-flag-12349 is the expert here 🙂
a
Hey @bright-dog-91720 great question! I agree that's tricky to have objects that are "almost VMs" like
Room
. Also, there's a lot of flexibility in using
AsyncSequence
/
Stream
or
Combine
vs SwiftUI-friendly primitives as you avoid storing some intermediate state. Currently, there are no plans for a direct rewrite (at least in 2.x). However, you can still re-wrap these objects and shape your state differently. You can take a look at some recent examples that map
@ObservableObject
via the publisher to your own model +
@Observable
macro.
BTW for very simple use cases, object like
Room
will still behave reactively:
Copy code
// Notify Room when state mutates
            Task { @MainActor in
                self.objectWillChange.send()
            }
you don't need a direct
@Published
thing in order to achieve that with the old Combine-based hack ⬆️
That's why the
.changes
wrapper works in my previous example 🙂 Achieving that with
@Observable
macro is not that easy as the "publishing" process is way more granular and sophisticated
In a complex scenario that you mentioned, some kind of "cherrypicking" from the state will always happen, regardless of the tool 😉
b
Thanks, I'll take a look the example. The usual issue with listening to an
ObservableObject
like this is that it's a
willChange
not a
didChange
and you don't have access to the actual change. Am I reading the code correctly that it ends up emitting
objectWillChange
once the state has actually been mutated so it has
didChange
semantics? By virtue of the change doing a context swap with the
Task
?
👍 1
a
Yes you're right, you miss the
oldValue
thing in this case
b
It'd probably be high throughput to build an
objectWillChange
-based publisher but
didChange
semantics are a winner for when we do want this. Thanks @able-flag-12349.
👍 1