realm or: how i learned to stop worrying and love my app database

50
Realm Learned to Stop Worryin g and Love my App or

Upload: sergi-martinez

Post on 16-Apr-2017

758 views

Category:

Mobile


0 download

TRANSCRIPT

Page 1: Realm or: How I learned to stop worrying and love my app database

RealmHow I

Learned to Stop

Worrying and

Love my App

Database

or

Page 2: Realm or: How I learned to stop worrying and love my app database

Sergi MartínezAndroid Developer at Schibsted Spain

Android GDE@sergiandreplace

Page 3: Realm or: How I learned to stop worrying and love my app database

The usual stuffMy big server

My big database

My mobile app

My little databas

e

My nice

API

The internet!

Page 4: Realm or: How I learned to stop worrying and love my app database

The usual stuffMy big server

My big database

My mobile app

My little databas

e

My nice

API

The internet!

This tries to mimic this

Page 5: Realm or: How I learned to stop worrying and love my app database

The usual stuffMy big server

My big database

My mobile app

My little databas

e

My nice

API

The internet!

Here We only need to cache entities

Page 6: Realm or: How I learned to stop worrying and love my app database

The initial solutionSQLite

Page 7: Realm or: How I learned to stop worrying and love my app database

SQLiteSQL languageRelationshipsNon-java data typesSQL QueriesSQL stuff

Page 8: Realm or: How I learned to stop worrying and love my app database

SQLiteSQL languageRelationshipsNon-java data typesSQL QueriesSQL stuff

Do we need these?

Page 9: Realm or: How I learned to stop worrying and love my app database

SQLiteSQL languageRelationshipsNon-java data typesSQL QueriesSQL stuff

Do we need these? Really?

Page 10: Realm or: How I learned to stop worrying and love my app database

Also...SQLDbHelperSQL QueriesContent ProvidersLoadersetc

Page 11: Realm or: How I learned to stop worrying and love my app database

Also...SQLDbHelperSQL QueriesContent ProvidersLoadersetc

Boilerplate

Page 12: Realm or: How I learned to stop worrying and love my app database

Also...SQLDbHelperSQL QueriesContent ProvidersLoadersetc

Boilerplate

Boilerplate

Page 13: Realm or: How I learned to stop worrying and love my app database

Also...SQLDbHelperSQL QueriesContent ProvidersLoadersetc

Boilerplate

Boilerplate

OH! Wow! So much boilerplate

Page 14: Realm or: How I learned to stop worrying and love my app database

Also...SQLDbHelperSQL QueriesContent ProvidersLoadersetc

Boilerplate

Boilerplate

OH! Wow! So much boilerplate

“Hard” to create. Excessive coupling with UI

Page 15: Realm or: How I learned to stop worrying and love my app database

Also...SQLDbHelperSQL QueriesContent ProvidersLoadersetc

Boilerplate

Boilerplate

OH! Wow! So much boilerplate

“Hard” to create. Excessive coupling with UI

Probably… more boilerplate

Page 16: Realm or: How I learned to stop worrying and love my app database

Let’s try something different… RealmIt stores objectsHierarchical relationships (sort of)No boilerplate (just probably mappers)Fast!Reduces dev time drastically

Page 17: Realm or: How I learned to stop worrying and love my app database

Adding Realm to your projectYour project build.gradlebuildscript { repositories { jcenter() } dependencies { classpath "io.realm:realm-gradle-plugin:0.89.0" }}

apply plugin: 'realm-android'

Your app build.gradle

Page 18: Realm or: How I learned to stop worrying and love my app database

Realm Objectspublic class Minion extends RealmObject {

private String name; private int eyes; private boolean goggles;

public String getName() { return name; } public void setName(String name) { this.name = name; }

public int getEyes() { return eyes; } public void setEyes(int eyes) { this.eyes = eyes; }

public boolean hasGoggles() { return goggles; } public void setGoggles(boolean goggles) { this.goggles = goggles; }}

Page 19: Realm or: How I learned to stop worrying and love my app database

Realm Objectspublic class Minion extends RealmObject {

private String name; private int eyes; private boolean goggles;

public String getName() { return name; } public void setName(String name) { this.name = name; }

public int getEyes() { return eyes; } public void setEyes(int eyes) { this.eyes = eyes; }

public boolean hasGoggles() { return goggles; } public void setGoggles(boolean goggles) { this.goggles = goggles; }}

Must extend RealmObject

Page 20: Realm or: How I learned to stop worrying and love my app database

Realm Objectspublic class Minion extends RealmObject {

private String name; private int eyes; private boolean goggles;

public String getName() { return name; } public void setName(String name) { this.name = name; }

public int getEyes() { return eyes; } public void setEyes(int eyes) { this.eyes = eyes; }

public boolean hasGoggles() { return goggles; } public void setGoggles(boolean goggles) { this.goggles = goggles; }}

Must extend RealmObjectNot exactly true since last thursday (#$%@!). More later...

Page 21: Realm or: How I learned to stop worrying and love my app database

Realm Objectspublic class Minion extends RealmObject {

private String name; private int eyes; private boolean goggles;

public String getName() { return name; } public void setName(String name) { this.name = name; }

public int getEyes() { return eyes; } public void setEyes(int eyes) { this.eyes = eyes; }

public boolean hasGoggles() { return goggles; } public void setGoggles(boolean goggles) { this.goggles = goggles; }}

Object members that will be storedMore on types later

Page 22: Realm or: How I learned to stop worrying and love my app database

Realm Objectspublic class Minion extends RealmObject {

private String name; private int eyes; private boolean goggles;

public String getName() { return name; } public void setName(String name) { this.name = name; }

public int getEyes() { return eyes; } public void setEyes(int eyes) { this.eyes = eyes; }

public boolean hasGoggles() { return goggles; } public void setGoggles(boolean goggles) { this.goggles = goggles; }}

Usual getters and setters

Page 23: Realm or: How I learned to stop worrying and love my app database

Supported data typesboolean/Booleanbyte/Byteshort/Shortint/Integerlong/Long

float/Floatdouble/DoubleStringDateByte[]

Page 24: Realm or: How I learned to stop worrying and love my app database

Storing an objectrealm.beginTransaction();Minion minion = realm.createObject(Minion.class);minion.setName("Bob");minion.setEyes(1);minion.setGoggles(true);realm.commitTransaction();

realm.cancelTransaction();

or

First, we start a transaction

Page 25: Realm or: How I learned to stop worrying and love my app database

Storing an objectrealm.beginTransaction();Minion minion = realm.createObject(Minion.class);minion.setName("Bob");minion.setEyes(1);minion.setGoggles(true);realm.commitTransaction();

realm.cancelTransaction();

or

Then we create an instance inside our realm

Page 26: Realm or: How I learned to stop worrying and love my app database

Storing an objectrealm.beginTransaction();Minion minion = realm.createObject(Minion.class);minion.setName("Bob");minion.setEyes(1);minion.setGoggles(true);realm.commitTransaction();

realm.cancelTransaction();

or

We set the values

Page 27: Realm or: How I learned to stop worrying and love my app database

Storing an objectrealm.beginTransaction();Minion minion = realm.createObject(Minion.class);minion.setName("Bob");minion.setEyes(1);minion.setGoggles(true);realm.commitTransaction();

realm.cancelTransaction();

or

And commit the transaction. Then, our changes are saved

Page 28: Realm or: How I learned to stop worrying and love my app database

Storing an objectrealm.beginTransaction();Minion minion = realm.createObject(Minion.class);minion.setName("Bob");minion.setEyes(1);minion.setGoggles(true);realm.commitTransaction();

realm.cancelTransaction();

or

Or we cancel the transaction and the new object is discarded

Page 29: Realm or: How I learned to stop worrying and love my app database

Storing an object - IIMinion minion = new Minion();minion.setName("Bob");minion.setEyes(1);minion.setGoggles(true);

realm.beginTransaction();realm.copyToRealm(minion);realm.commitTransaction();

Another way. We create the object outside the realm

Page 30: Realm or: How I learned to stop worrying and love my app database

Storing an object - IIMinion minion = new Minion();minion.setName("Bob");minion.setEyes(1);minion.setGoggles(true);

realm.beginTransaction();realm.copyToRealm(minion);realm.commitTransaction();

Then, we begin a transaction, copy the object into the realm, and commit

Page 31: Realm or: How I learned to stop worrying and love my app database

Obtaining your RealmFirst, create your Realm configurationRealmConfiguration config = new RealmConfiguration.Builder(context) .name("myrealm.realm") .encryptionKey(getKey()) .schemaVersion(42) .setModules(new MySchemaModule()) .migration(new MyMigration()) .build();

Page 32: Realm or: How I learned to stop worrying and love my app database

Obtaining your RealmThen, get an instanceRealmConfiguration config = new RealmConfiguration.Builder(context) .name("myrealm.realm") .encryptionKey(getKey()) .schemaVersion(42) .setModules(new MySchemaModule()) .migration(new MyMigration()) .build();

Realm myRealm = Realm.getInstance(config);

Page 33: Realm or: How I learned to stop worrying and love my app database

Obtaining your RealmOr use the default Realm InstanceRealmConfiguration config = new RealmConfiguration.Builder(context) .name("myrealm.realm") .encryptionKey(getKey()) .schemaVersion(42) .setModules(new MySchemaModule()) .migration(new MyMigration()) .build();

Realm.setDefaultConfiguration(config);

And obtain it anywhere Realm realm = Realm.getDefaultInstance(); // Stuff realm.close();

Page 34: Realm or: How I learned to stop worrying and love my app database

Close your RealmsFor each

Realm.getDefaultInstance()

or

Realm.getInstance()

you must execute realm.close() at the end

Page 35: Realm or: How I learned to stop worrying and love my app database

Querying a RealmRealm uses fluent syntax. Just create a RealmQuery object based on the Realm class you wantRealmQuery<Minion> query = realm.where(Minion.class);

Add operations and filters and executeRealmResults<Minion> results = query.equalTo(“name”, “Bob”).findAll();

Do something with the resultfor (Minion minion:results) {…. //Do stuff}

Page 36: Realm or: How I learned to stop worrying and love my app database

Query operatorsbetween()greaterThan()lessThan()greaterThanOrEqual()lessThanOrEqual()

equalTo()notEqualTo()contains()beginsWith()endsWith()

Page 37: Realm or: How I learned to stop worrying and love my app database

Annotations@Index

@PrimaryKey

@Ignore

@Required

Page 38: Realm or: How I learned to stop worrying and love my app database

Relationships - 1:NJust have a RealmObject with a RealmObjectpublic class Minion extends RealmObject { private String name; private int eyes; private boolean goggles; private Banana banana

//Getters, setters and so on

}

Public class Banana extends RealmObject { …}

Page 39: Realm or: How I learned to stop worrying and love my app database

Storing 1:N related objectsrealm.beginTransaction()

Banana banana = realm.createObject(Banana.class);banana.setSomething…

Minion minion = realm.createObject(Minion.class);minion.setName(“Bob”);minion.setBanana(banana);

realm.writeTransaction();

Page 40: Realm or: How I learned to stop worrying and love my app database

Relationships - M:NJust have a RealmObject with a List<? extends RealmObject>public class Minion extends RealmObject { private String name; private int eyes; private boolean goggles; private RealmList<Banana> bananas

//Getters, setters and so on

}

Public class Banana extends RealmObject { …}

Page 41: Realm or: How I learned to stop worrying and love my app database

Storing M:N related objectsrealm.beginTransaction()

Banana banana = realm.createObject(Banana.class);banana.setSomething…

Minion minion = realm.createObject(Minion.class);minion.setName(“Bob”);minion.getBananas().add(banana);

realm.writeTransaction();

Page 42: Realm or: How I learned to stop worrying and love my app database

Querying M:N related objectsRealmResults<Minion> minions = realm.where(Minions.class) .equalTo("banana.size", "big") .findAll();

Page 43: Realm or: How I learned to stop worrying and love my app database

ThreadingEach Realm should live in the same Thread is has been created

You can’t pass Realms between threads

Realms are automatically updated between threads IF they are in threads with a Looper

Otherwise… Realm.refresh()

Page 44: Realm or: How I learned to stop worrying and love my app database

More thingsDynamic realms

Schemas

Migrations

UI controls

Browsers

etc...

Page 45: Realm or: How I learned to stop worrying and love my app database

Brand new changesThis week on Realm:

RealmModel interface + static methods

RealmCollection/RealmOrderedCollection Interfaces

Primary keys can be null!

Page 46: Realm or: How I learned to stop worrying and love my app database

How I use itpublic abstract class VOBaseMapper<V extends RealmObject,E> {

public abstract V toVO(E entity); public abstract E toEntity(V vo) ;

public RealmList<V> toVO(List<E> entities) { RealmList<V> realmList=new RealmList<V>(); for (E entity:entities) { realmList.add(toVO(entity)); } return realmList; }

Page 47: Realm or: How I learned to stop worrying and love my app database

How I use it public List<E> toEntity(RealmList<V> vos) { List<E> applicationEntityList = new ArrayList<>(); for(V vo :vos){ applicationEntityList.add(toEntity(vo)); } return applicationEntityList; }

public List<E> toEntity(RealmResults<V> vos) { List<E> applicationEntityList = new ArrayList<>(); for(V vo :vos){ applicationEntityList.add(toEntity(vo)); } return applicationEntityList; }}

Page 48: Realm or: How I learned to stop worrying and love my app database

How I use itpublic class UserVOMapper extends VOBaseMapper<UserVO,User > {

private User user;

@Override public UserVO toVO(User user) { UserVO vo=new UserVO(); vo.setName(user.name); vo.setEmail(user.email); return vo; }

@Override public User toEntity(UserVO vo) { User user= new User(); user.name=vo.getName(); user.email=vo.getEmail(); return user; }}

Page 49: Realm or: How I learned to stop worrying and love my app database

And that’s not allBut enough for today.Questions?

Page 50: Realm or: How I learned to stop worrying and love my app database

@sergiandreplace

Thanks for coming