Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Realm with RxJava

Using Realm with RxJava

Using Realm with RxJava

Daichi Furiya (Wasabeef)

April 23, 2015
Tweet

More Decks by Daichi Furiya (Wasabeef)

Other Decks in Programming

Transcript

  1. 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); });
  2. 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) { } });
  3. issue #865 ! // Get observable of all users Realm.getInstance(context)

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

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

    ! public User(String name) { this.name = name; } ! public String getName() { return name; } }
  6. 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); }
  7. 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); } }); } }
  8. 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
  9. 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); } }