KVO listener issues in Swift 4

2019-07-07 16:44发布

问题:

I am using ViewModel class and want to setup observer if any changes into loginResponse variable.

@objcMembers class ViewModel: NSObject {

    var count = 300
    @objc dynamic var loginResponse :String

    override init() {
        loginResponse = "1"
        super.init()
        setupTimer()
    }

    func setupTimer(){
        _ = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector:#selector(callTimer), userInfo: nil, repeats: true)
    }

    func callTimer(){
        let minutes = String(count / 60)
        let seconds = String(count % 60)
        loginResponse = minutes + ":" + seconds
        count =  count - 1
    }
}

View controller code:

override func viewDidLoad() {
    super.viewDidLoad()

    _ = viewModel.observe(\ViewModel.loginResponse) { (model, changes) in
        print(changes)
    }
}

I want to listen to any change into loginResponse variable in my Viewcontroller but it's not getting the callback. What am I doing wrong here?

回答1:

The .observe(_:options:changeHandler:) function returns a NSKeyValueObservation object that is used to control the lifetime of the observation. When it is deinited or invalidated, the observation will stop.

Since your view controller doesn't keep a reference to the returned "observation" it goes out of scope at the end of viewDidLoad and thus stops observing.

To continue observing for the lifetime of the view controller, store the returned observation in a property. If you're "done" observing before that, you can call invalidate on the observation or set the property to nil.