Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an Observable in Android?

What I want to do is to create a simple in-memory cache just to try Observables out. However I got stuck because I don't understand how to create an observable. This is the code I have gotten so far:

public class MovieCache {
    MovieWrapper movieWrapper;

    public Observable<MovieWrapper> getMovies() {
       //How to create and return an Observable<MovieWrapper> here?
    }

    public void setCache(MovieWrapper wrapper) {
        movieWrapper = wrapper;
    }

    public void clearCache() {
        movieWrapper = null;
    }
}

In the getMovies() method I want to create an Observable and return my local field movieWrapper to the subscriber. How can I do this? I tried with using new Observable.just(movieWrapper) but it results in a null exception.

like image 945
Deichmann Dacke Andersson Avatar asked Mar 07 '16 16:03

Deichmann Dacke Andersson


People also ask

How do you make an Observable?

The process to create an Observable is fairly straightforward. First of all, we need to import Observable from rxjs. Then, we create an Observable by calling the new Observable constructor that takes one argument. In the following example, we create an Observable that emits a number every second to a subscriber.

What is an Observable in Android?

Observability refers to the capability of an object to notify others about changes in its data. The Data Binding Library allows you to make objects, fields, or collections observable. Any plain-old object can be used for data binding, but modifying the object doesn't automatically cause the UI to update.

How do you make a class Observable on Android?

To make a property observable, use @Bindable annotation on getter method. Call notifyPropertyChanged(BR. property) in setter method to update the UI whenever the data is changed. The BR class will be generated automatically when data binding is enabled.


1 Answers

Take a look at this tutorial as it does exactly what you are looking for. Basically you use defer() to make sure you always get the latest version of your cached object:

public class MovieCache {
    MovieWrapper movieWrapper;

    public Observable<MovieWrapper> getMovies() {  
        return Observable.defer(new Func0<Observable<MovieWrapper>>() {
            @Override
            public Observable<MovieWrapper> call() {
                return Observable.just(movieWrapper);
            }
        });
    }

    public void setCache(MovieWrapper wrapper) {
        movieWrapper = wrapper;
    }

    public void clearCache() {
        movieWrapper = null;
    }
}

defer() makes sure that you will get the object upon subscription to the Observable not on creation.

Note however that, according to the author of the post:

The only downside to defer() is that it creates a new Observable each time you get a subscriber. create() can use the same function for each subscriber, so it's more efficient. As always, measure performance and optimize if necessary.

like image 119
Ricardo Avatar answered Sep 20 '22 21:09

Ricardo