Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO listener issues in Swift 4

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?

like image 226
Chandan kumar Avatar asked Dec 07 '25 06:12

Chandan kumar


1 Answers

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.

like image 85
David Rönnqvist Avatar answered Dec 09 '25 19:12

David Rönnqvist