SwiftUI View subscribes all publishers that ObservableObject has (objectWillChange is not required)

Updated
Sep 19, 2019 2:00 PM
Created
Sep 16, 2019 6:43 AM
Tags
SwiftUIMemo
Keywords
Date

struct PublishedContentView: View {
  
  @ObservedObject private var store: Store = .init()
  
  var body: some View {
    Group {
      HStack {
        Button(action: {
          self.store.fire1()
        }, label: {
          Text("Fire 1")
        })
        Button(action: {
          self.store.fire2()
        }, label: {
          Text("Fire 2")
        })
      }
      HStack {
        Text(store.value1.description)
        Text(store.value2.description)
      }
    }
  }
}

fileprivate final class Store: ObservableObject {
  
  @Published private(set) var value1: Int = 0
  @Published private(set) var value2: Int = 0
  
  func fire1() {
    value1 += 1
  }
  
  func fire2() {
    value2 += 1
  }
}