Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update property without .observeNext #660

Open
Kingamattack opened this issue Oct 10, 2019 · 1 comment
Open

Update property without .observeNext #660

Kingamattack opened this issue Oct 10, 2019 · 1 comment

Comments

@Kingamattack
Copy link

Hi,

I have this property in my viewModel, that change the value of my UIImage if my method returns true of false

var image: Observable<UIImage?> {
        if musicService.isPlaying() {
            return Observable<UIImage?>(UIImage(named: "ic_pause"))
        } else {
            return Observable<UIImage?>(UIImage(named: "ic_play"))
        }
    }

Want I want is to be able to update the value of my property and notify its observers that the value changed (or didn't change) without having to image.ObserveNext { } cause I don't have any additional processing to do with the closure.

Example:

func foo() {
// Do something
// image.UPDATE()
}

So basically I want to know if there is another method that I can call, to pass again in my property, return the right image and notify all the observers

@mattboran
Copy link

I'm not a contributor on this repository but I do use it at work, and I think you can think about this a little differently. What you're trying to observe seems to be the state of musicService, whether or not it's playing. I assume you're trying to set a UIImageView.image based on this state?

It would make more sense to have musicService.isPlaying be an Observable<Bool>. Then, you could bind that to a field in your viewModel, like so:

musicService.isPlaying
    .map { isPlaying in
        isPlaying ? UIImage(named: "ic_pause") : UIImage(named: "ic_play")
    }.bind(to: image)

Then, whenever you start or stop the music service, you can update isPlaying.value and the image will automatically be updated, and so will any UI components that you have that are observing image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants