I am trying to perform some operatations when a activity onDestroy
is called. I want start a Observable with a ID, then retrieve some data from Realm and execute a HTTP request to a backend based on the retrieved data and afterwards store the retrieved data to the row given with the starting id.
Summary:
- Retrieve data from database with id
- Use data to perform a request to backend
- Store retrieved data to row with the id from step 1
Graphical:
Code: what I ended up with and got stuck
Observable.just(id)
.observeOn(Schedulers.io())
.map(new Function<String, Person>() {
@Override
public Person apply(@NonNull String id) throws Exception {
Realm realm = Realm.getDefaultInstance();
Person person = realm.copyFromRealm(realm.where(Person.class).equalTo("id", id).findFirst());
realm.close();
return person;
}
})
.switchMap(new Function<Person, Observable<Directions>>() {
@Override
public Observable<Directions> apply(@NonNull Person person) throws Exception {
return Utils.getRemoteService().getDirections(person.getAddress()); // retrofit
}
})
.map(new Function<Directions, Object>() {
@Override
public Object apply(@NonNull Directions directions) throws Exception {
// how do I get the id here to store the data to the correct person
return null;
}
})
.subscribe();
Note:
- POJO's are fictive
- It's my first time using RxJava