Slide 1

Slide 1 text

Using Realm with RxJava Wasabeef

Slide 2

Slide 2 text

About Me wasabeef CyberAgent, Inc.

Slide 3

Slide 3 text

Realm Latest v0.80.1 (2015.4.23)

Slide 4

Slide 4 text

Introduction

Slide 5

Slide 5 text

Realm

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

RealmObject public class User extends RealmObject { /** Other fields… **/ }

Slide 8

Slide 8 text

RealmList public class User extends RealmObject { private RealmList emails; /** Other fields… **/ }

Slide 9

Slide 9 text

RealmQuery RealmQuery query = realm.where(User.class); query.equalTo(“name”, “Wasabeef”);

Slide 10

Slide 10 text

RealmResults RealmResults result = query.findAll();

Slide 11

Slide 11 text

RxJava

Slide 12

Slide 12 text

Observable String[] names = {“Wasabeef”, “Chip”}; ! Observable.from(names).subscribe(s -> { Log.d(“Hello ” + s); });

Slide 13

Slide 13 text

map, flatMap… etc. Observable.from(names) .subscribeOn(Schedulers.newThread()) .map(new Func1() { @Override public String call(String name) { return name.toUpperCase(); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Log.d(“Hello ” + s); });

Slide 14

Slide 14 text

subscribe(onNext, onError..) Observable.from(names).subscribe( new Observer() { @Override public void onCompleted() { } ! @Override public void onError(Throwable e) { } ! @Override public void onNext(String s) { } });

Slide 15

Slide 15 text

Wrapping Realm around RxJava Source: http://kboyarshinov.com/android/wrapping-realm-around-rxjava/

Slide 16

Slide 16 text

issues #865

Slide 17

Slide 17 text

issue #865 ! // Get observable of all users Realm.getInstance(context) .where(User.class) .findAll() .observable() .subscribe(users -> { // do stuff with users });

Slide 18

Slide 18 text

Defining Realm model public class RealmUser extends RealmObject { private String name; ! public String getName() { return name; } ! public void setName(String name) { this.name = name; } }

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Observing Realm

Slide 21

Slide 21 text

public abstract class OnSubscribeRealm implements Observable.OnSubscribe { ! /** 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); }

Slide 22

Slide 22 text

public final class RealmObservable { ! private RealmObservable() { } ! public static Observable object( Context context, Func1 function) { ! return Observable.create(new OnSubscribeRealm(context) { @Override public T get(Realm realm) { return function.call(realm); } }); } }

Slide 23

Slide 23 text

public class RealmDataService { private Context context; ! public RealmDataService(Context context) { this.context = context.getApplicationContext(); } ! public Observable addUser(String userName) { return RealmObservable.object(context, new Func1() { @Override public RealmUser call(Realm realm) { RealmUser realmUser = new RealmUser(); realmUser.setName(userName); return realm.copyToRealm(realmUser); } }).map(new Func1() { @Override public User call(RealmUser realmUser) { // map to UI object return new User(realmUser.getName()); } }); } } Data Service

Slide 24

Slide 24 text

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); } }

Slide 25

Slide 25 text

Wasabeef

Slide 26

Slide 26 text

CyberAgent, Inc.

Slide 27

Slide 27 text

Thanks.