Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Using Realm with RxJava
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Daichi Furiya (Wasabeef)
April 23, 2015
Programming
2.5k
13
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Using Realm with RxJava
Using Realm with RxJava
Daichi Furiya (Wasabeef)
April 23, 2015
More Decks by Daichi Furiya (Wasabeef)
See All by Daichi Furiya (Wasabeef)
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
6
2.9k
About Flutter Architecture
wasabeef
1
320
2023 Flutter/Dart Summary
wasabeef
0
140
I/O Extended 2023 - Dart と Flutter の新機能
wasabeef
0
240
I/O Extended 2023 - Flutter 活用事例
wasabeef
10
3.1k
What it Takes to be a Flutter Developer
wasabeef
0
260
FlutterKaigi 2022 Keynote
wasabeef
1
730
Flutter Hooks を使ったアプリ開発 / App Development with the Flutter Hooks
wasabeef
2
1.5k
Flutter 2021 の振り返りと今後のアプリ開発に向けて / Looking back on Flutter 2021 and for future app development.
wasabeef
4
2.2k
Other Decks in Programming
See All in Programming
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
860
数百円から始めるRuby電子工作
tarosay
0
140
生成AI導入の「期待外れ」を乗り越える ー 開発フロー改革が目指す、真の組織変革
starfish719
0
4.1k
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
140
FDEが実現するAI駆動経営の現在地
gonta
2
270
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
210
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
380
AI時代に設計が 最大の生産性レバーになる 意図駆動開発とデータを消さない設計|Don't Delete Your Data or Your Intent — Design as the Deepest Lever in the AI Era
tomohisa
1
730
源内ハンズオン概要編
hideg
0
130
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
430
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
140
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
550
Featured
See All Featured
Visualization
eitanlees
152
17k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
We Have a Design System, Now What?
morganepeng
55
8.3k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
610
My Coaching Mixtape
mlcsv
0
210
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
370
Facilitating Awesome Meetings
lara
57
7.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
650
Skip the Path - Find Your Career Trail
mkilby
1
180
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
Transcript
Using Realm with RxJava Wasabeef
About Me wasabeef CyberAgent, Inc.
Realm Latest v0.80.1 (2015.4.23)
Introduction
Realm
Realm.getInstance() Realm realm = Realm.getInstance(this);
RealmObject public class User extends RealmObject { /** Other fields…
**/ }
RealmList<T> public class User extends RealmObject { private RealmList<Email> emails;
/** Other fields… **/ }
RealmQuery<T> RealmQuery<User> query = realm.where(User.class); query.equalTo(“name”, “Wasabeef”);
RealmResults<T> RealmResults<User> result = query.findAll();
RxJava
Observable<T> String[] names = {“Wasabeef”, “Chip”}; ! Observable.from(names).subscribe(s -> {
Log.d(“Hello ” + s); });
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); });
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) { } });
Wrapping Realm around RxJava Source: http://kboyarshinov.com/android/wrapping-realm-around-rxjava/
issues #865
issue #865 ! // Get observable of all users Realm.getInstance(context)
.where(User.class) .findAll() .observable() .subscribe(users -> { // do stuff with users });
Defining Realm model public class RealmUser extends RealmObject { private
String name; ! public String getName() { return name; } ! public void setName(String name) { this.name = name; } }
Defining Immutable POJO public class User { private String name;
! public User(String name) { this.name = name; } ! public String getName() { return name; } }
Observing Realm
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); }
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); } }); } }
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
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); } }
Wasabeef
CyberAgent, Inc.
Thanks.