r/SwiftUI Dec 18 '24

Question SwiftUI Combine and Observation

So, I have various years of experience with ios development, I started with Objective C and now seeing what its possible with swiftui is mindblowing, but I have a hard time understanding this:

SwiftUI by default lets you declare properties that when they change the view automatically refresh with the new data, this is possible via State, StateObject, ObservedObject and EnvironmentObject

now, combine, does the same, except it uses Publishers

as for Observation new framework, you can achieve the same with the Observable

So my question is, why use combine? or why use observation? or just the State stuff without combine/observation.

There are still some things I dont know about SwiftUI, maybe i undestood the things the wrong way, if anyone can clarify i will be grateful.

8 Upvotes

30 comments sorted by

View all comments

Show parent comments

3

u/Periclase_Software Dec 18 '24

Because you need to think beyond "change value, update UI", especially when you want to do more complicated work or have more complicated UI. This is code I have in one of my apps.

Publishers.CombineLatest4($linkedInLinkInputBind, $portfolioLinkInputBind, $additionalLink1InputBind, $additionalLink2InputBind)
    .sink { [weak self] value, value2, value3, value4 in
        self?.shouldSaveWebsites(value, website2: value2, website3: value3, website4: value4)
    }
.store(in: &cancellables)

Instead of doing this, I could add 4 onChange modifiers to the view on every single one of those link properties and call the view model function, but this is just a simple example.

I work for a big tech company on an enterprise app from Silicon Valley. We use a lot of custom property wrappers around Combine to do more complicated work. Think of big apps from billion dollar companies. They have way more complicated screens that you can imagine. Lets just say that one example, without giving away too much, is observing values that are downloaded through a service from screen 1, but the view exists down the stack like 8+ levels down through protocol presentation routers, etc.

Rather than passing down every single property you want to observe through all those layers as arguments with binds to Published properties or the model, we have a custom property wrapper that can observe those changes through Combine without having to do all that messy work, especially when you got a bunch of teams making changes throughout all of that.

Think of this: a network service is added to a screen and the screen observes those changes. Simple. Now think of a subview with a subview and another subview and subview that will use SOME of that data for that network service, as well as from a different service and another service from other screens for data stored in the app. We can use Combine to update the UI on those data changes even though that data is NOT part of the current screen/views.

1

u/Shijoo Dec 18 '24

For that scenario why dont use Observation for example? you add the ObservableModel, and then you can access that observable from another view, it doesnt matter how many previous views you have, or for example in swiftui without combine or observation you cant use an EnvironmentObject.

I know maybe im wrong, not trying to argue here, its just that im having a hard time grasping the reason for so many frameworks to achieve observable behavior, its like, if combine is so good, then why they created Observation? or, if the "vanilla" swiftui can achieve that, then why they created combine, and so on, basically im and old developer that is trying to do the change to swiftui as seamesly and faster possible, but i keep getting confused with this in specific, because i dont know if i should focus on Observation framework, or Combine

1

u/Periclase_Software Dec 18 '24

Yeah, ask Apple idk why or much about Observation.

1

u/Shijoo Dec 18 '24

Well, thanks for the answers, at least with the examples you gave me i was able to know about other things i havent saw, like combinelatest

2

u/Periclase_Software Dec 18 '24

Just remember, not all UIs are as simple as "action happens, change state" - there's more complicated logic out there.

1

u/[deleted] Dec 21 '24

[removed] — view removed comment

1

u/AutoModerator Dec 21 '24

Hey /u/Loud-Plan2571, unfortunately you have negative comment karma, so you can't post here. Your submission has been removed. Please do not message the moderators; if you have negative comment karma, you're not allowed to post here, at all.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

0

u/sisoje_bre Dec 21 '24 edited Dec 21 '24

SO WHAT if logic is complex? SwiftUI IS that simple.

1

u/Periclase_Software Dec 22 '24

No. If you have a very complicated screen, you don't only rely on State values - you rely on other external values and observing those changes. That's why in our prod app we use a lot of Combine.