manishkkatoch / SimpleTwoWayBindingIOS

An ultra light, ultra simple two way binding library for IOS and Swift.
MIT License
165 stars 48 forks source link

Observable doesn't support optional ObservedType #10

Open reuvenlevitsky opened 5 years ago

reuvenlevitsky commented 5 years ago

Hey, right now this library has Observable which might support on theory observing optional types. The problem is that when the value is being set, you have this check:

    public var value: ObservedType? {
        didSet {
            if let value = value {
                notifyObservers(value)
            }
        }
    }

That doesn't allow the observing instance to react to the "cleanup" of this model. For example if ObservedType = UIImage, and I want to clean up the image from the UIImageView:

viewModel.thumbnailImage.bind { [weak self] _ , image in
            self?.imageView.image = image
        }

This code will not be invoked for a nil value.

Do you have any plans to support setting nil value to Observable?

manishkkatoch commented 5 years ago

Yes, as of now, it doesn't support the notion of null(nil) as a valid value. There needs to be an explicit way of un-setting the value and notify observers. which would also put the onus on observers to be vary of fact that the value recieved can be nil. I still need to figure out if this should be part of the default behavior and if yes, should it be a strong abstraction (Optional value?)