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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Michal Moczulski
November 06, 2014
Programming
990
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
340
Espresso UI Testing on Android
mrmike
5
550
Beyond Java - Kotlin in Android Development
mrmike
1
500
Other Decks in Programming
See All in Programming
PyConJP2026_wat_Python × Signal Processing: How to Draw Pictures with Sound Using Spectrogram Art
wat
0
630
不幸な GC
chencmd
0
840
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.5k
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
48
30k
Press start. Python's next generation.
willingc
PRO
3
290
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
190
[DroidKaigi 26] How We Moved from Android Studio to Slack
thisaay
0
120
FastAPI の並行処理モデルを完全に理解する
hoto17296
9
3.3k
S3 を使うアプリケーションをローカル完結で動かすことに全力を注いでみた / Running S3 Apps Offline
contour_gara
0
720
Oxlintはいいぞ(続)
yug1224
1
500
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
0
190
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
480
Featured
See All Featured
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.5k
New Earth Scene 8
popppiees
3
2.5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
310
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
800
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
430
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
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!