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.3k
ConcatAdapterを深掘る
androhi
1
360
Android Studio 4.1推しポイント!
androhi
0
1.2k
一人開発でつまづいたときの処方箋
androhi
0
290
Androidの物理ベースアニメーション
androhi
1
550
ConstraintLayout再入門
androhi
2
3.4k
Firebase Analytics 使用感
androhi
0
850
Support Library v23.2 overview
androhi
0
640
Support Library 総復習
androhi
2
2.4k
Other Decks in Programming
See All in Programming
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
CSC509 Lecture 12
javiergs
PRO
0
160
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
Ethereum_.pdf
nekomatu
0
460
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
役立つログに取り組もう
irof
28
9.6k
CSC509 Lecture 11
javiergs
PRO
0
180
イベント駆動で成長して委員会
happymana
1
320
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
Better Code Design in PHP
afilina
PRO
0
120
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
The Language of Interfaces
destraynor
154
24k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Designing for Performance
lara
604
68k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
For a Future-Friendly Web
brad_frost
175
9.4k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Building Your Own Lightsaber
phodgson
103
6.1k
Teambox: Starting and Learning
jrom
133
8.8k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
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Λͦͷ··͏ͷ͕ྑͦ͞͏