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
970
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
200
AutoValue
mrmike
0
290
Espresso UI Testing on Android
mrmike
5
500
Beyond Java - Kotlin in Android Development
mrmike
1
480
Other Decks in Programming
See All in Programming
GraphQLサーバの構成要素を整理する #ハッカー鮨 #tsukijigraphql / graphql server technology selection
izumin5210
4
910
Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | AndroidMakers Paris
prof18
0
160
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
200
Apache Hive 4 on Treasure Data
ryukobayashi
1
430
Try creating your own orderedmap
kazamori
1
230
MicrosoftのPlatform Engineeringガイドを読んで実際になにかやってみた
ymd65536
1
520
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
9
4.3k
Native Federation: The Future of Micro Frontends in Angular
manfredsteyer
PRO
0
110
TCAとKMPを用いた新規動画配信アプリ 「ABEMA Live」の設計
tomu28
2
130
PHPはいつから死んでいるかの調査
chiroruxx
2
420
Git Lint
bkuhlmann
4
760
Hanami and htmx
bkuhlmann
0
230
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1025
450k
Rails Girls Zürich Keynote
gr2m
91
13k
Build The Right Thing And Hit Your Dates
maggiecrowley
25
2k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
21
1.6k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.1k
Into the Great Unknown - MozCon
thekraken
15
1k
Designing the Hi-DPI Web
ddemaree
276
33k
Six Lessons from altMBA
skipperchong
22
3k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
79
43k
Product Roadmaps are Hard
iamctodd
45
9.8k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
9
1.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
126
32k
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!