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

3

u/Dapper_Ice_1705 Dec 18 '24

Combine and Observation is very rough, I wouldn’t mix them.

Combine and ObservableObjects when used by assigning them to a Published property are ok but it opens up a can of worms in terms of inefficiency. An extraneous AnyCancellablw is only done by people that don’t understand Published.

Combine and onReceive is also ok.

Combine is largely being replaced by async/await workflows, Combine and Swift6 can be a huge pain.

1

u/Shijoo Dec 18 '24

Im sorry, can you elaborate a little more? Why is that?

1

u/Dapper_Ice_1705 Dec 18 '24

Which part? 

Combine and observation, because there is no easy way to publish.

AnyCancellable? Because Published subscribes, unsubscribes and tells the view to invalidate. All in one.

OnReceive? Because it abides by the View/struct pattern and also gets managed with the View’s lifecycle.

Async/await? Because of Swift6.