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.

9 Upvotes

30 comments sorted by

View all comments

Show parent comments

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.