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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Michal Moczulski
November 06, 2014
Programming
980
2
Share
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
320
AutoValue
mrmike
0
320
Espresso UI Testing on Android
mrmike
5
540
Beyond Java - Kotlin in Android Development
mrmike
1
490
Other Decks in Programming
See All in Programming
Mastering Event Sourcing: Your Parents Holidayed in Yugoslavia
super_marek
0
150
3分でわかるatama plusのQA/about atama plus QA
atamaplus
0
130
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
170
Don't Prompt Harder, Structure Better
kitasuke
0
670
実践ハーネスエンジニアリング #MOSHTech
kajitack
7
6.2k
条件判定に名前、つけてますか? #phperkaigi #c
77web
2
1k
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
3
270
PCOVから学ぶコードカバレッジ #phpcon_odawara
o0h
PRO
0
250
ドメインイベントでビジネスロジックを解きほぐす #phpcon_odawara
kajitack
2
130
飯MCP
yusukebe
0
490
CursorとClaudeCodeとCodexとOpenCodeを実際に比較してみた
terisuke
1
400
我々はなぜ「層」を分けるのか〜「関心の分離」と「抽象化」で手に入れる変更に強いシンプルな設計〜 #phperkaigi / PHPerKaigi 2026
shogogg
2
910
Featured
See All Featured
Facilitating Awesome Meetings
lara
57
6.8k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
150
Google's AI Overviews - The New Search
badams
0
970
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
520
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
790
Color Theory Basics | Prateek | Gurzu
gurzu
0
290
Discover your Explorer Soul
emna__ayadi
2
1.1k
Building a Scalable Design System with Sketch
lauravandoore
463
34k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
350
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
68
38k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
690
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!