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

Hands on Realm Mobile Platform

Hands on Realm Mobile Platform

Talk at GDG DevFest 2016 in Düsseldorf.

Realm Mobile Platform is a brand new tool by Realm. It was just released less than one month ago and instantly gained a lot of attention among the mobile development community. It combines the popular Realm Mobile Database with the complete new Realm Object Server. Its objective is to become the perfect backend for the next generation of reactive mobile apps. But what’s behind this promise? How does it work? What’s so new and why should you care?

André Diermann

October 23, 2016
Tweet

More Decks by André Diermann

Other Decks in Programming

Transcript

  1. FEATURES Free Pro manage all your passwords ( ( protected

    by 512-bit encryption ( ( create custom fields ( ( realtime cloud sync ) ( protected cloud backup ) (
  2. QUIZ HOW MANY LINES OF CODE? Feature Lines of code

    Manage passwords (create/read/update/delete) 5 Encryption 1 Custom fields (create/read/update/delete) 3 Synchronization (sign-in, sign-up, sync) 4
  3. REALM Realm is an open source, cross-platform mobile database technology

    used by more than a billion people worldwide.
  4. REALM MILESTONES When? What? Jul 2014 launched Realm May 2016

    released 1.0.0 Sep 2016 introduced Realm Mobile Platform
  5. SETUP //project level build.gradle buildscript { repositories { ... }

    dependencies { classpath "io.realm:realm-gradle-plugin:2.0.2" } } //application level build.gradle apply plugin: 'realm-android'
  6. REALMS a Realm is a database it contains different kinds

    of objects which are mapped to one file on disk Realms are configurable and thread-safe
  7. EXAMPLE // Initialize Realm in your Application Realm.init(this); RealmConfiguration config

    = new RealmConfiguration.Builder().build(); Realm.setDefaultConfiguration(config); // Obtain and close a Realm instance in your Activity realm = Realm.getDefaultInstance(); doSomeStuff(); realm.close();
  8. MODELS //Option 1: Extending RealmObject public class Password extends RealmObject

    {...} password.addChangeListener(listener); //Option 2: Implementing RealmModel @RealmClass public class Password extends SpecialClass implements RealmModel {...} RealmObject.addChangeListener(password, listener);
  9. MODELS Field types, annotations, auto-updating public class Password extends RealmObject

    { @PrimaryKey private int id; private Date created; private byte[] icon; @Required private String password; @Ignore private long indexInList; // ... }
  10. RELATIONS Any two Realm models can be linked together public

    class Password extends RealmObject { // ... // one-to-one and many-to-one private Category category; // one-to-many and many-to-many private RealmList<CustomField> customFields; // ... }
  11. READING DATA // queries are lazy (data is never copied)

    realm.where(Password.class).findAll(); // fluent interface allows convenient multi-clause queries realm.where(Password.class) .equalTo("id", id) .greaterThan("created", today) .findFirst() // it's possible to query relationships as well realm.where(Password.class) .contains("fields.name", "Amazon") .findAll();
  12. WRITING DATA // write operations must be wrapped in transactions

    realm.beginTransaction(); // allow solid data state realm.copyToRealm(password) // transactions can either be committed or cancelled realm.commitTransction(); // use transaction blocks realm.executeTransaction(realm1 -> realm1.copyToRealm(password));
  13. EXCURSUS (UN)MANAGED OBJECTS // Unmanaged Password password = new Password(...);

    password.setUsername("user"); //this change is not persisted realm.executeTransaction(realm1 -> realm1.copyToRealm(password)); // Managed Password password; realm.executeTransaction(realm1 -> { password = realm1.createObject(Password.class); password.setUsername("user"); //this change is persisted //and propagated }); password.setPassword("pass"); //this is not allowed
  14. UPDATES RealmObjects are live and auto-updating never have to be

    refreshed modifying objects update query results immediately.
  15. THREADING Realm works across threads each thread always has a

    consistent view of the data illegal states are impossible
  16. ENCRYPTION // should be stored in and obtained from Android

    keystore byte[] key = new byte[64]; new SecureRandom().nextBytes(key); RealmConfiguration config = new RealmConfiguration.Builder() .encryptionKey(key) .build(); Realm realm = Realm.getInstance(config);
  17. MIGRATION When schema changes over time // 1. default: exception

    is thrown // 2. simple: new RealmConfiguration.Builder() .deleteRealmIfMigrationNeeded() .build(); // 3. custom: new RealmConfiguration.Builder() .schemaVersion(2) .migration(migrataion) .build();
  18. MIGRATION RealmMigration migration = new RealmMigration() { @Override public void

    migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema(); if (oldVersion++ == 0) { // adapt schema from version 0 to 1 } if (oldVersion++ == 1) { // adapt schema from version 1 to 2 } // more versions } }
  19. BETA NOTICE Realm Mobile Platform is in beta state. API

    may change without further notice.
  20. SIGN-UP / SIGN-IN // registration Credentials credentials = Credentials.usernamePassword(username, password,

    true); // built-in: username / password Credentials credentials = Credentials.usernamePassword(username, password, false); // third-party: Google, Facebook (, iCloud) Credentials google = Credentials.google(token); Credentials facebook = Credentials.facebook(token); // authentication String authURL = "http://my.realm-auth-server.com:9080/auth"; User user = User.login(credentials, authURL);
  21. CONCLUSION Realm really speeds up app development very fast learning

    curve most convenient and mature DB replacement* there are alternatives: Firebase, ...
  22. TESTING DO NOT TEST REALM - TEST YOUR BL //

    use in-memory Realm RealmConfiguration config = new RealmConfiguration.Builder() .inMemory().name("test-realm").build(); realm = Realm.getInstance(config); // Assign - Act - Assert Password expected = createDummyPassword(); copyToRealm(expected); Password actual = interactor.readPassword(1); assertThat(actual, equalTo(expected));
  23. RXJAVA is a first-class citizen in Realm just combine Obeservables

    from Realm with other sources, e.g. Retrofit Observable<Password> passwordsFromRealm = realm.where(Password.class).findAllAsync().asObservable(); Observable<Password> passwordsFromRetrofit = passwordService.loadPasswords(); Observable.mergeDelayError( passwordsFromRealm, passwordsFromRetrofit) .subscribe(...);