+ All Categories
Home > Documents > REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes...

REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes...

Date post: 20-Jun-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
61
REALM.IO NOSQL FOR MOBILE
Transcript
Page 1: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

REALM.IO NOSQL FOR MOBILE

Page 2: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

Mitchell Tilbrook▸ Twitter: @sir_tilbrook▸ Mobile Engineer @ Seatfrog▸ Android, iOS, Xamarin

▸ Recording & Editing for 7+ meet-ups▸ ANZCoders: https://www.youtube.com/c/

ANZCoders▸ Android Sydney: http://bit.ly/2dvqk2t

Page 3: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

WHY?

Page 4: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

1. Not always FAST2. Threading is hard

3. Writes can block reads4. Stringly typed schema

5. Object Mapping6. Migrations

Page 5: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations
Page 6: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

DATA MODELS

Page 7: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ANDROID MODELSpublic class Person extends RealmObject { private String name; private int age;

// Getters and Setters}

OR

Page 8: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ANDROID INTERFACE MODELS@RealmClasspublic class Person implements RealmModel { private String name; private int age;

// Getters and Setters …}

// with RealmObjectperson.isValid();// With RealmModelRealmObject.isValid(user);

Page 9: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

PROPERTY ATTRIBUTES

Page 10: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ANDROID PRIMARY KEYpublic class Person extends RealmObject { @PrimaryKey private int id; private String name; private Int age;

// Getters and Setters}

Page 11: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

REQUIREDpublic class Person extends RealmObject { @Required private String name; @Required private Int age;

// Getters and Setters}

Page 12: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

INDEXINGpublic class Person extends RealmObject { @Index private String name; private int age;

// Getters and Setters}

Page 13: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

DON'T PERSIST PROPERTIESpublic class User extends RealmObject { private String email; @Ignore private String sessionToken;

// Getters and Setters}

Page 14: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

CUSTOM MODEL LOGICpublic class Person extends RealmObject { private String name; private int age;

public String getName() { return name.toUpperCase(); }

public String getNameAndAge() { return name + " is " + age + " old"; }

public void setName(String newName) { name = newName.toUpperCase(); }}

Page 15: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

QUERYING// Gets All the Users RealmQuery<Person> query = realm.where(Person.class);

Page 16: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

QUERYING// Gets All the Users RealmQuery<Person> query = realm.where(Person.class);

// Add query conditions:query.equalTo("name", "John");query.or().equalTo("name", "Peter");

// Execute the query:RealmResults<Person> result1 = query.findAll();

// Or alternatively do the same all at once (the "Fluent interface"):RealmResults<Person> result2 = realm.where(Person.class) .equalTo("name", "John") .or() .equalTo("name", "Peter") .findAll();

Page 17: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

MANY-TO-ONEpublic class Person extends RealmObject { // .... private Dog dog;

// Getters and Setters }

Page 18: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

MANY-TO-MANYpublic class Person extends RealmObject { // .... private RealmList<Dog> dogs;

// Getters and Setters }

Page 19: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

RECURSIVE RELATIONSHIPSpublic class Person extends RealmObject { public String name; public RealmList<Person> friends; // Other fields…}

public class Person extends RealmObject { public String name; public Person father; // Other fields…}

Page 20: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

LINKED QUERIESpublic class Person extends RealmObject { private String id; private String name; private RealmList<Dog> dogs; // getters and setters}

public class Dog extends RealmObject { private String id; private String name; private String color; // getters and setters}

Page 21: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

LINKED QUERIES

Page 22: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

LINKED QUERIESRealmResults<Person> persons = realm .where(Person.class) .equalTo("dogs.color", "Brown") .findAll();

Page 23: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

QUERYING IS LAZYRealmResults<Person> result = realm .where(Person.class) .findAll();

Page 24: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

READING LAZY DATAPerson person = results.get(1);

String name = person.getName();

Page 25: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ASYNC QUERYINGprivate RealmChangeListener callback = new RealmChangeListener() { @Override public void onChange(RealmResults<Person> results) { // called once the query complete and on every update }};

public void onStart() { RealmResults<Person> result = realm.where(Person.class).findAllAsync(); result.addChangeListener(callback);}

Page 26: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

AUTO UPDATINGRealmResults<Person> peoples = realm.where(Person.class).findAll();peoples.size(); // => 0

addRandomPerson();

peoples.addChangeListener( results -> { // results and peoples point are both up to date results.size(); // => 1 peoples.size(); // => 1});

Page 27: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

CHANGE EVENTS BUBBLERealmResults<Person> peoples = realm.where(Person.class).findAll();

peoples.addChangeListener( results -> { /* ... */ } );

realm.addChangeListener( results -> { /* ... */ } );

Page 28: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

FINE GRAIN CHANGE LISTENERS

Page 29: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

SAVING UNMANAGED OBJECTSPerson obj = new Person();obj.setId(42);obj.setName("Fish");

// Obtain a Realm instanceRealm realm = Realm.getDefaultInstance();// Save to diskrealm.beginTransaction();managedPerson = realm.copyToRealmOrUpdate(obj);realm.commitTransaction();

Page 30: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

SAVING MANAGED OBJECTS// Obtain a Realm instanceRealm realm = Realm.getDefaultInstance();realm.beginTransaction();Person person = realm.createObject(Person.class);person.setName("Mitchell");person.setAge(27);realm.commitTransaction();

Page 31: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ROLLBACK FAILED TRANSACTIONStry { realm.beginTransaction(); Person person = realm.createObject(Person.class); person.setName("Mitchell"); person.setAge(27); realm.commitTransaction();} catch(Exception ex) { // Rollback realm.cancelTransaction();}

Page 32: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

RECOMMENDED TRANSACTION STYLEouterRealm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { Person person = realm.createObject(Person.class); person.setName("Mitchell"); person.setAge(27); }});

Page 33: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ASYNCHRONOUS TRANSACTIONSrealm.executeTransactionAsync({ // ...}, new Realm.Transaction.OnSuccess() { @Override public void onSuccess() { // Transaction was a success. }}, new Realm.Transaction.OnError() { @Override public void onError(Throwable error) { // Transaction failed and was automatically canceled. }});

Page 34: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

DELETE TRANSACTIONSrealm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { person.getDog().deleteFromRealm(); person.deleteFromRealm(); }});

Page 35: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

AGGREGATIONRealmResults<User> results = realm.where(User.class).findAll();long sum = results.sum("age").longValue();long min = results.min("age").longValue();long max = results.max("age").longValue();double average = results.average("age");

long matches = results.size();

Page 36: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations
Page 37: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ENCRYPTION// Totally not safe keyval key = new byte[64];new SecureRandom().nextBytes(key);val config = new RealmConfiguration.Builder() .encryptionKey(key) .build();

// Open the Realm with encryption enabledval realm = Realm.getInstance(config);

Page 38: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

DATA MIGRATIONS

Page 39: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

JUST DELETE ITRealmConfiguration config = new RealmConfiguration.Builder() .deleteRealmIfMigrationNeeded() .build()

Page 40: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

MIGRATIONSRealmConfiguration config = new RealmConfiguration.Builder() .schemaVersion(2) // Must be bumped when the schema changes .migration(new MyMigration()) // Migration to run instead of throwing an exception .build()

Page 41: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

MIGRATIONSRealmMigration migration = new RealmMigration() { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema();

if (oldVersion == 0) { schema.create("Person") .addField("name", String.class) .addField("age", int.class); oldVersion++; }

if (oldVersion == 1) { schema.get("Person") .addField("id", long.class, FieldAttribute.PRIMARY_KEY) .addRealmObjectField("favoriteDog", schema.get("Dog")) .addRealmListField("dogs", schema.get("Dog")); oldVersion++; } }}

Page 42: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

REALM SETUP

Page 43: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ANDROID SETUPbuildscript { repositories { jcenter() } dependencies { classpath "io.realm:realm-gradle-plugin:2.0.0" }}

apply plugin: 'realm-android'

Page 44: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ANDROID INITpublic class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Realm.init(this); RealmConfiguration config = new RealmConfiguration.Builder() .build(); Realm.setDefaultConfiguration(config) }}

Page 45: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

SWIFT & OBJECTIVE-C▸ https://realm.io/docs/swift/latest/#installation▸ https://realm.io/docs/objc/latest/#installation

Page 46: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

REALM BROWSER

Page 47: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations
Page 48: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

REALM MOBILE PLATFORM

Page 49: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

REALM USER SEGREGATION

Page 50: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

DRAWBACKS▸ Thread confined*▸ Large binary

▸ Connections need to be closed

Page 51: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ANDROID SIZE COST

Page 52: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

IOS SIZE COST

Page 53: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

INSPECT REALM ON ANDROID

Realm.init(this);

Stetho.initialize( Stetho.newInitializerBuilder(this) .enableDumpapp( Stetho.defaultDumperPluginsProvider(this) ) .enableWebKitInspector( RealmInspectorModulesProvider .builder(this) .build() ) .build());

Page 54: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

ANDROID LIBRARY INTEGRATIONS▸ JSON ( GSON, Moshi )

▸ RxJava▸ Retrofit

Page 55: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

RXJAVA SUPPORTrealm.where(Person.class) .findAllAsync() .asObservable();

Page 56: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

RXJAVA REALM SCHEDULERfun Schedulers.newRealmThread(): Scheduler { val backgroundThread = HandlerThread( "RxRealm-BackgroundThread", THREAD_PRIORITY_BACKGROUND ) backgroundThread.start() val backgroundLooper = backgroundThread.looper return AndroidSchedulers.from(backgroundLooper)}

compile "io.reactivex:rxandroid:$rx_android_version"

Page 57: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

PRODUCTION READY?

Page 58: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations
Page 59: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

YES

Page 60: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

QUESTIONS?

Page 61: REALM.IO NOSQL FOR MOBILE - YOW! Conferences · 1. Not always FAST 2. Threading is hard 3. Writes can block reads 4. Stringly typed schema 5. Object Mapping 6. Migrations

Mitchell Tilbrook ! @SIR_TILBROOK " GITHUB.COM/MARUKAMI


Recommended