Last active
September 11, 2016 13:27
-
-
Save ccjeng/a3aebb2bbdef317ab68587cd46691f0a to your computer and use it in GitHub Desktop.
Realm rxjava sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Observable<List<Person>> getPersons() { | |
final Realm realm = Realm.getDefaultInstance(); | |
return realm.where(Person.class).findAll() //get Persons from DB | |
.asObservable() | |
.map(new Func1<RealmResults<Person>, List<Person>>() { | |
@Override | |
public List<Person> call(RealmResults<Person> person) { | |
return realm.copyToRealm(person); //Convert RealmResults to List | |
} | |
}).doOnUnsubscribe(new Action0() { | |
@Override | |
public void call() { | |
realm.close(); | |
} | |
}); | |
} | |
repository.getPersons() | |
.doOnNext(new Action1<List<Person>>() { | |
@Override | |
public void call(List<Person> persons) { | |
personView.addPersons(persons); //Add data to ListView | |
} | |
}).flatMapIterable(new Func1<List<Person>, Iterable<Person>>() { | |
@Override | |
public Iterable<Person> call(List<Person> persons) { | |
return persons; | |
} | |
}).flatMap(new Func1<Person, Observable<Person>>() { | |
@Override | |
public Observable<Person> call(Person person) { | |
return repository.getGitHubData(person); //get each persons github data | |
} | |
}).subscribe(new Subscriber<Person>() { | |
@Override | |
public void onCompleted() { | |
this.unsubscribe(); | |
} | |
@Override | |
public void onError(Throwable e) { | |
Log.e(TAG, e.getMessage()); | |
} | |
@Override | |
public void onNext(Person person) { | |
personView.updatePerson(person); //Update data to ListView | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment