Using Realm with RxJava

Using Realm with RxJava

Using Realm with RxJava

6dd0483f1353a4a359e92633cfd65c64?s=128

Daichi Furiya (Wasabeef)

April 23, 2015
Tweet

Transcript

  1. Using Realm with RxJava Wasabeef

  2. About Me wasabeef CyberAgent, Inc.

  3. Realm Latest v0.80.1 (2015.4.23)

  4. Introduction

  5. Realm

  6. Realm.getInstance() Realm realm = Realm.getInstance(this);

  7. RealmObject public class User extends RealmObject { /** Other fields…

    **/ }
  8. RealmList<T> public class User extends RealmObject { private RealmList<Email> emails;

    /** Other fields… **/ }
  9. RealmQuery<T> RealmQuery<User> query = realm.where(User.class); query.equalTo(“name”, “Wasabeef”);

  10. RealmResults<T> RealmResults<User> result = query.findAll();

  11. RxJava

  12. Observable<T> String[] names = {“Wasabeef”, “Chip”}; ! Observable.from(names).subscribe(s -> {

    Log.d(“Hello ” + s); });
  13. map, flatMap… etc. Observable.from(names) .subscribeOn(Schedulers.newThread()) .map(new Func1<String, String>() { @Override

    public String call(String name) { return name.toUpperCase(); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Log.d(“Hello ” + s); });
  14. subscribe(onNext, onError..) Observable.from(names).subscribe( new Observer<String>() { @Override public void onCompleted()

    { } ! @Override public void onError(Throwable e) { } ! @Override public void onNext(String s) { } });
  15. Wrapping Realm around RxJava Source: http://kboyarshinov.com/android/wrapping-realm-around-rxjava/

  16. issues #865

  17. issue #865 ! // Get observable of all users Realm.getInstance(context)

    .where(User.class) .findAll() .observable() .subscribe(users -> { // do stuff with users });
  18. Defining Realm model public class RealmUser extends RealmObject { private

    String name; ! public String getName() { return name; } ! public void setName(String name) { this.name = name; } }
  19. Defining Immutable POJO public class User { private String name;

    ! public User(String name) { this.name = name; } ! public String getName() { return name; } }
  20. Observing Realm

  21. public abstract class OnSubscribeRealm<T extends RealmObject> implements Observable.OnSubscribe<T> { !

    /** constructor **/ ! @Override public void call(final Subscriber<? super T> subscriber) { Realm realm = Realm.getInstance(context); subscriber.add(Subscriptions.create(() -> { try { realm.close(); } catch (RealmException ex) { subscriber.onError(ex); } })); ! T object; realm.beginTransaction(); try { object = get(realm); realm.commitTransaction(); } catch (RuntimeException e) { realm.cancelTransaction(); subscriber.onError(new RealmException(Error during transaction., e)); return; } catch (Error e) { realm.cancelTransaction(); subscriber.onError(e); return; } if (object != null) { subscriber.onNext(object); } subscriber.onCompleted(); } ! public abstract T get(Realm realm); }
  22. public final class RealmObservable { ! private RealmObservable() { }

    ! public static <T extends RealmObject> Observable<T> object( Context context, Func1<Realm, T> function) { ! return Observable.create(new OnSubscribeRealm<T>(context) { @Override public T get(Realm realm) { return function.call(realm); } }); } }
  23. public class RealmDataService { private Context context; ! public RealmDataService(Context

    context) { this.context = context.getApplicationContext(); } ! public Observable<User> addUser(String userName) { return RealmObservable.object(context, new Func1<Realm, RealmUser>() { @Override public RealmUser call(Realm realm) { RealmUser realmUser = new RealmUser(); realmUser.setName(userName); return realm.copyToRealm(realmUser); } }).map(new Func1<RealmUser, User>() { @Override public User call(RealmUser realmUser) { // map to UI object return new User(realmUser.getName()); } }); } } Data Service
  24. Usage private void addUser() { Subscription subscription = dataService.addUser(“Wasabeef”) .subscribeOn(Schedulers.io())

    .observeOn(AndroidSchedulers.mainThread()) .subscribe( user -> /** use user data **/, throwable -> /** error! **/ ); if (compositeSubscription != null) { compositeSubscription.add(subscription); } }
  25. Wasabeef

  26. CyberAgent, Inc.

  27. Thanks.