I got this sample code about ObservableObject from Apple official website
import Foundation
class Contact: ObservableObject {
@Published var name: String
@Published var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func changeAge() -> Int {
self.age += 1
return self.age
}
}
class Test {
init() {
let john = Contact(name: "John Appleseed", age: 24)
_ = john.objectWillChange
.sink { _ in
print("\(john.age) will change")
}
print(john.changeAge())
}
}
let test = Test()
When running on terminal by swift Contact.swift, the result is only 25, but official website shows the result should be
// Prints "24 will change"
// Prints "25"
Why the first line Prints "24 will change" is not shown?
Thank you.
You have to store the AnyCancellable that's returned by sink, otherwise it cancels the subscription as soon as it's deinitialized when you assign to _.
In your simple example just assigning to a local variable is enough to get the printout that you want, because the variable still lives when you change the age:
let c = john.objectWillChange.sink {...}
But in a real app, you'd want to assign it to a property, which would keep the subscription for the duration that you need. Typically, it's done like this:
class Test {
private var c: Set<AnyCancellable> = []
init() {
let john = Contact(name: "John Appleseed", age: 24)
john.objectWillChange
.sink { _ in
print("\(john.age) will change")
}
.store(in: &c) // store here
print(john.changeAge())
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With