How to use RxAndroid?

0

I'm trying to use RxAndroid but I can not figure it out.

In reality, I wanted to be able to use methods within an Observable, to access the FirebaseFirestore and retrieve the data into Realm.

So the question is: How do I create observables that return methods?

    
asked by anonymous 23.11.2017 / 12:18

1 answer

1

By your explanation and your final question I see two scenarios. And without seeing your code just to make abstract examples, then come on.

First scenario: Observables working with methods

To pass "methods" in Observable , you will have to create a class or interface that declares this method and use it. The same way it would be done without the Observable.

Example with Callable :

// Type could be something else
Callable<String> callHello = new Callable<String>() {
    @Override
    public String call() throws Exception {
        // Could be a db query, api call... 
        return "Hello";
    }
};

Callable<String> callWorld = new Callable<String>() {
    @Override
    public String call() throws Exception {
        return "World";
    }
};

The type of the first Observable would be Callable<String> , as follows:

Observable.just(callHello, callWorld)
        .map(new Function<Callable<String>, String>() {
            @Override
            public String apply(Callable<String> callable) throws Exception {
                return callable.call();
            }
        })
        .subscribe(new Consumer<String>() {
            @Override
            public void accept(String x) throws Exception {
                System.out.println(x);
            }
        });

An example with a repository of Model

I declare the contract:

interface ModelRepository<T extends Model> {
    List<T> listModels();
}

I have two classes that obey this contract. A search User (which extends Model ):

class UserRepository implements ModelRepository<User> {

    @Override
    public List<User> listModels() {
        return userDAO.list();
    }
}

Another search Event (which also extends Model ):

class EventRepository implements ModelRepository<Event> {

    @Override
    public List<Event> listModels() {
        return eventDAO.list();
    }
}

The Observable then starts with the repositories and ends with two lists of Model

Observable.just(userRepository, eventRepository)
        .map(ModelRepository::listModels)
        .subscribe(listOfModels -> {/* do something with List<Model> */});

In both cases I'm passing an object that implements a method declared in the interfaces ( call and listModels ).

Second scenario: Involve existing APIs in Observables

For blocking methods the simplest way is to use fromCallable :

Observable.fromCallable(() -> blockingGetUserFromRemote())
            .flatMap(user -> Observable.fromCallable(() -> blockingSaveUserToLocal(user)))
            .subscribe(saveResult -> { /* Do something with save result */ });

In this case I have two blocking methods: blockingGetUserFromRemote that searches for a user in a webservice and blockingSaveUserToLocal that saves the user in the database. With fromCallable you can wrap them in Callable and use them in a non-blocking way.

To turn listeners into Observables, I recommend seeing the source code of some library that does this ( RxBinding , for example). It's a bit more complex and long.

    
23.11.2017 / 14:00