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
Michal Moczulski
November 06, 2014
Programming
2
980
When SQLite is not enough - Realm database
Quick introduction to Realm database on Android.
Michal Moczulski
November 06, 2014
Tweet
Share
More Decks by Michal Moczulski
See All by Michal Moczulski
Continuous integration with Docker (Android app example)
mrmike
0
280
AutoValue
mrmike
0
320
Espresso UI Testing on Android
mrmike
5
530
Beyond Java - Kotlin in Android Development
mrmike
1
490
Other Decks in Programming
See All in Programming
機能追加とリーダー業務の類似性
rinchoku
2
1.3k
請來的 AI Agent 同事們在寫程式時,怎麼用 pytest 去除各種幻想與盲點
keitheis
0
120
アセットのコンパイルについて
ojun9
0
130
AWS発のAIエディタKiroを使ってみた
iriikeita
1
190
Ruby×iOSアプリ開発 ~共に歩んだエコシステムの物語~
temoki
0
320
Testing Trophyは叫ばない
toms74209200
0
880
テストコードはもう書かない:JetBrains AI Assistantに委ねる非同期処理のテスト自動設計・生成
makun
0
320
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
120
「待たせ上手」なスケルトンスクリーン、 そのUXの裏側
teamlab
PRO
0
530
速いWebフレームワークを作る
yusukebe
5
1.7k
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
450
時間軸から考えるTerraformを使う理由と留意点
fufuhu
16
4.8k
Featured
See All Featured
Embracing the Ebb and Flow
colly
87
4.8k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Thoughts on Productivity
jonyablonski
70
4.8k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Build The Right Thing And Hit Your Dates
maggiecrowley
37
2.9k
Building Applications with DynamoDB
mza
96
6.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Typedesign – Prime Four
hannesfritz
42
2.8k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
The World Runs on Bad Software
bkeepers
PRO
70
11k
Building an army of robots
kneath
306
46k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
840
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!