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
When SQLite is not enough - Realm database
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Michal Moczulski
November 06, 2014
Programming
980
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
When SQLite is not enough - Realm database
Quick introduction to Realm database on Android.
Michal Moczulski
November 06, 2014
More Decks by Michal Moczulski
See All by Michal Moczulski
Continuous integration with Docker (Android app example)
mrmike
0
330
AutoValue
mrmike
0
330
Espresso UI Testing on Android
mrmike
5
540
Beyond Java - Kotlin in Android Development
mrmike
1
500
Other Decks in Programming
See All in Programming
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
930
これからAgentCoreを触る方へトレンドはGatewayです
har1101
6
450
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
130
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
180
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.5k
Creating Composable Callables in Contemporary C++
rollbear
0
180
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
230
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
1k
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.1k
Featured
See All Featured
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
760
The untapped power of vector embeddings
frankvandijk
2
1.8k
Building Adaptive Systems
keathley
44
3.1k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.8k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
2
280
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
72
40k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
220
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Code Reviewing Like a Champion
maltzj
528
40k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Transcript
When SQLite is not enough - Realm database Michał Moczulski
@moczul
Agenda • Realm • Installation • Models • Object creation
• Internals • Queries • Relationships • Sample app • Summary
Realm • Mobile oriented database • Android and iOS support
• Simple API (no ORM needed!) • Memory efficient • Realm browser • Since 2011
Installation dependencies { compile 'io.realm:realm-android:0.71.0' ... }
Models public class Place extends RealmObject { @Index private String
name; private double lat; private double lng; @Ignore private String description; public void setName(String name) { this.name = name; } public String getName() { return name; } }
Object creation Realm realm = Realm.getInstance(getBaseContext()); realm.beginTransaction(); for (Item item
: items) { Place place = realm.createObject(Place.class); place.setName(item.venue.name); place.setLat(item.venue.location.lat); place.setLng(item.venue.location.lng); } realm.commitTransaction();
Internals • Proxy class • io.realm." + modelClassName + “RealmProxy
• getters & setters without extra logic • Sample generated class
Queries - SQL way protected List<Place> getPlaces(LatLngBounds bounds) { SQLiteDatabase
db = mSqlHelper.getReadableDatabase(); final String[] columns = new String[] {FIELD_NAME, FIELD_LAT, FIELD_LNG}; final String[] selectionArgs = new String[] {bounds.southwest.latitude, bounds.northeast.latitude, bounds.southwest.longitude, bounds.northeast.longitude}; Cursor cursor = db.query(TABLE_NAME, columns, FIELD_LAT + " > ? AND " + FIELD_LAT + " < ? AND " + FIELD_LNG + " > ? AND " + FIELD_LNG + " < ?", selectionArgs, null, null, null); try { List<Place> places = new ArrayList<Place>(); for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex(FIELD_NAME)); double lat = cursor.getDouble(cursor.getColumnIndex(FIELD_LAT)); double lng = cursor.getDouble(cursor.getColumnIndex(FIELD_LNG)); Place place = new Place(); place.setName(name); place.setLat(lat); place.setLng(lng); places.add(place); } return places; } finally { if (cursor != null) { cursor.close(); } }
Queries - Realm way protected List<Place> getPlaces(LatLngBounds bounds) { RealmResults<Place>
results = mRealm.where(Place.class) .between("lat", bounds.southwest.latitude, bounds.northeast.latitude) .between("lng", bounds.southwest.longitude, bounds.northeast.longitude) .findAll(); return results; }
Queries RealmQuery<Place> query = mRealm.where(Place.class) .between() .greaterThan() .lessThan() .greaterThanOrEqualTo() .lessThanOrEqualTo()
.equalTo() .notEqualTo() .contains() .beginsWith() .endsWith(); RealmResults<Place> results = query.findAll(); RealmResults<Place> first = query.findFirst();
Relationships public class City extends RealmObject { private String name;
private RealmList<Place> places; } public class Place extends RealmObject { ... }
Sample app
Summary • Great API • Some limitations • Fast (but
Sqlite is fast enough in most cases) • Worth to try!
Resources • http://realm.io/ • http://realm.io/docs/java/0.73.0/ • https://github.com/realm/realm-java • https://github.com/realm/realm-java/wiki/Current- limitations
Questions?
Thank you!