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
Realm with ContentProvider
Search
Takahiro Shimokawa
April 23, 2015
Programming
8
5.1k
Realm with ContentProvider
I tried to implement realm-java with ContentProvider.
Takahiro Shimokawa
April 23, 2015
Tweet
Share
More Decks by Takahiro Shimokawa
See All by Takahiro Shimokawa
PlayStoreでの新しいユーザー訴求 -LiveOpsの活用とその成果-
androhi
0
2.4k
ConcatAdapterを深掘る
androhi
1
380
Android Studio 4.1推しポイント!
androhi
0
1.3k
一人開発でつまづいたときの処方箋
androhi
0
310
Androidの物理ベースアニメーション
androhi
1
560
ConstraintLayout再入門
androhi
2
3.4k
Firebase Analytics 使用感
androhi
0
860
Support Library v23.2 overview
androhi
0
650
Support Library 総復習
androhi
2
2.4k
Other Decks in Programming
See All in Programming
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
1
190
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
250
Amazon Nova Reelの可能性
hideg
0
240
ISUCON14感想戦で85万点まで頑張ってみた
ponyo877
1
780
Lookerは可視化だけじゃない。UIコンポーネントもあるんだ!
ymd65536
1
140
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
2.3k
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
3
620
Vue.jsでiOSアプリを作る方法
hal_spidernight
0
120
DMMオンラインサロンアプリのSwift化
hayatan
0
230
functionalなアプローチで動的要素を排除する
ryopeko
1
770
最近のVS Codeで気になるニュース 2025/01
74th
1
230
2024年のkintone API振り返りと2025年 / kintone API look back in 2024
tasshi
0
160
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
521
39k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
4 Signs Your Business is Dying
shpigford
182
22k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
260
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Optimising Largest Contentful Paint
csswizardry
33
3k
The Language of Interfaces
destraynor
156
24k
Transcript
Realm with ContentProvider Realm meetup #2
About me 4 Լ ܟ߂ (@androhi) 4 גࣜձࣾZaim (AndroidΞϓϦ୲) 4
DroidKaigiͰൃද͠·͢ 4 JellyBeanͱKitKatͰ࣮ݱ͢ΔϚςϦΞϧσβΠϯ
Tried ContentProviderͰΞΫηε͢ΔDBΛɺRealmʹͯ͠Έ·͠ ͨ Reason AndroidͷSyncAdapterΛͬͨࣗಈಉظͷΈͰɺό οΫάϥϯυͰಉظॲཧΛ͢ΔࡍʹContentProviderΛ༻ ͍Δͱ͍͏ϧʔϧ͕͋ΔͨΊ
None
Extend ContentProvider 1. Queryϝιουͷ࣮ (+ ಠࣗCursorͷੜ) 2. Insertϝιουͷ࣮ 3. Updateϝιουͷ࣮
4. Deleteϝιουͷ࣮ 5. BulkInsertϝιουͷ࣮
This time 1. Queryϝιουͷ࣮ (+ ಠࣗCursorͷੜ) 2. Insertϝιουͷ࣮ 3. Updateϝιουͷ࣮
4. Deleteϝιουͷ࣮ 5. BulkInsertϝιουͷ࣮
1-1. Create cursor static final String[] sColumns = new String[]
{"_id", "name", "price"}; RealmQuery<Item> query = mRealm.where(Item.class); RealmResults<Item> results = query.findAll(); MatrixCursor matrixCursor = new MatrixCursor(sColumns); for (Item item : results) { Object[] rowData = new Object[]{item.get_id(), item.getName(), item.getPrice()}; matrixCursor.addRow(rowData); }
1-2. Implement query @Override public Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) { ... RealmQuery<Item> query = mRealm.where(Item.class); RealmResults<Item> results = query.findAll(); MatrixCursor matrixCursor = new MatrixCursor(sColumns); for (Item item : results) { Object[] rowData = new Object[]{item.get_id(), item.getName(), item.getPrice()}; matrixCursor.addRow(rowData); } return matrixCursor; }
2. Implement insert @Override public Uri insert(Uri uri, ContentValues contentValues)
{ ... mRealm.beginTransaction(); Item item = mRealm.createObject(Item.class); item.set_id(++count); item.setName(contentValues.getAsString(sColumns[1])); item.setPrice(contentValues.getAsLong(sColumns[2])); mRealm.commitTransaction(); return Uri.withAppendedPath(uri, String.valueOf(item.get_id())); }
5. Implement bulkInsert @Override public int bulkInsert(Uri uri, ContentValues[] values)
{ ... mRealm.beginTransaction(); try { for (ContentValues value : values) { Item item = mRealm.createObject(Item.class); item.set_id(value.getAsLong(sColumns[0])); item.setName(value.getAsString(sColumns[1])); item.setPrice(value.getAsLong(sColumns[2])); } } finally { mRealm.commitTransaction(); } return values.length; }
compare with SQLite Query/Insert -> 10,000݅ Xperia Z1f (Android 4.4.2)
SQLite with ContentProvider 4 Insertɺ͔ͳΓ͍ 4 Query͕ૣ͍
Realm with ContentProvider 4 Insert͕ɺͦΜͳʹ͘ͳ͍ 4 Query͕SQLiteʹൺΔͱ͍ ʢCursorੜͷӨڹʣ 4 BulkInsertޓ֯ʁ
None
None
None
Summary 4 ύϑΥʔϚϯεམͪΔʢʁʣ͚ͲContentProviderͰ ͑Δ 4 Queryϝιουվળͷ༨͕͋Γͦ͏ 4 ࣗಈಉظ͚ͩContentProviderܦ༝ʹ͠ɺΞϓϦຊମ RealmΛͦͷ··͏ͷ͕ྑͦ͞͏