Upgrade to Pro — share decks privately, control downloads, hide ads and more …

When SQLite is not enough - Realm database

When SQLite is not enough - Realm database

Quick introduction to Realm database on Android.

Avatar for Michal Moczulski

Michal Moczulski

November 06, 2014
Tweet

More Decks by Michal Moczulski

Other Decks in Programming

Transcript

  1. Agenda • Realm • Installation • Models • Object creation

    • Internals • Queries • Relationships • Sample app • Summary
  2. Realm • Mobile oriented database • Android and iOS support

    • Simple API (no ORM needed!) • Memory efficient • Realm browser • Since 2011
  3. 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; } }
  4. 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();
  5. Internals • Proxy class • io.realm." + modelClassName + “RealmProxy

    • getters & setters without extra logic • Sample generated class
  6. 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(); } }
  7. 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; }
  8. 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();
  9. Relationships public class City extends RealmObject { private String name;

    private RealmList<Place> places; } public class Place extends RealmObject { ... }
  10. Summary • Great API • Some limitations • Fast (but

    Sqlite is fast enough in most cases) • Worth to try!