How to watch changes on fields of `@Published FamilyActivitySelection`?

class MyModel: ObservableObject {
  @Published var selection = FamilyActivitySelection()

  init() {
    $selection.sink {
      newSelection in
      print(newSelection)
    }
  }
}

class MyView: View {
  @StateObject var model = MyModel()
  // some body
  // .... 

  // my method
  func removeToken(token: ApplicationToken) {
    model.selection.applicationTokens.remove(token)
  }
}

I am using the above code. When I call removeToken, the callback from the sink (which is registered in init() of MyModel) is called without any changes. newSelection still contains the token that I removed.

Currently, I am using the additional code below to work around the problem.

.onChange(of: model.selection.applicationTokens) {
  newSet in
  model.selection.applicationTokens = newSet
}

Should I use the workaround solution, or am I missing something?

Answered by Dirk-FU in 778279022

You have to keep a reference to the Cancellable returned by sink:

import Combine

class MyModel: ObservableObject {
    @Published var selection = FamilyActivitySelection()
    var cancellables = Set<AnyCancellable>()
    
    init() {
        $selection.sink {
            newSelection in
            print(newSelection)
        }.store(in: &cancellables)
    }
}
Accepted Answer

You have to keep a reference to the Cancellable returned by sink:

import Combine

class MyModel: ObservableObject {
    @Published var selection = FamilyActivitySelection()
    var cancellables = Set<AnyCancellable>()
    
    init() {
        $selection.sink {
            newSelection in
            print(newSelection)
        }.store(in: &cancellables)
    }
}
How to watch changes on fields of `@Published FamilyActivitySelection`?
 
 
Q