skip to Main Content

I attached an image. Please see it.

1

As far as I know the "View" is only view. It’s not controller.
So Im developing like Way 1.
But I faced a problem that how can I use observableobject in another observableobject?

I thought if I pass a parameter with the observableobject the problem will be clean.
But I think it is bad way..

So I thought way 2.
But the way is the "View" is not only view. It is view and controller.

So Im confused the way2 is bad way or not.

Which way is good way? and Im wondering other SwiftUI developers how to develop about this case.

Please advice me if you think there is better way than way1 & way2.

Summary

Q1. Way1 – How can I use observableobject in another observableobject? (singltone? like static shared)

Q2. Way2 – Is it correct way? (View = view + controller)

Q3. Your advice.

Env

Xcode 14.2

Swift 5.7.2

2

Answers


  1. Here is the sample code for your question:

    struct MainView: View {
         @StateObject var mainVM = MainViewModel()
         @ObservedObject var discoveryVM:DiscoveryViewModel
    
         var body: some View {
               ZStack {
                    ScrollView {
                         // if data changed in DiscoveryViewModel view will automatically upadte
                         ForEach(discoveryVM.showProfiles, id: .self) { profile in
                              MyView(profile: Profile)
                         }
                    }
               }
               .onReceive(discoveryVM.$toggle) { status in 
               // change something in MainViewModel when toggle in DiscoveryViewModel
                    mainVM.toggle = status // change variable(rearly used)
                    mainVM.doSomething() // call fucntions
               }
         }
    }
    
    
    class MainViewModel: ObservableObject {
         @Published var toggle: Bool = false
         
         func doSomething() {
            print("Do something")
         }
    }
    
    
    
    class DiscoveryViewModel: ObservableObject {
         @Published var data: [ProfileModel] = []
         @Published var toggle: Bool = false
    }
    

    ObservableObjects are mostly used where there is nesting of views

    Login or Signup to reply.
  2. SwiftUI is a new architecture, old design patterns don’t really fit. In SwiftUI the View struct isn’t the view in the MVC sense, that view layer (e.g. UIView/NSView objects) is generated automatically for us based diffing the View structs which are super fast efficient value types. View state is held in property wrappers like @State which essentially makes the value behave like an object, e.g. it has a "memory". You can pass this down to sub-Views as let for read access or @Binding var for write access, SwiftUI will track dependencies and call body on the affected View structs when necessary.

    Normally in SwiftUI we only have a single ObservableObject that holds the model structs in @Published properties. Usually there are 2 singletons, one shared that loads from disk and another preview pre-loaded with test data for driving SwiftUI previews. This object is shared across View structs using .environmentObject

    If you attempt to use multiple ObservableObject for view data instead of @State you’ll run into major problems, actually the kind of consistency problems that SwiftUI’s use of value types was designed to eliminate.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search