In a world where users have ever higher expectations from the apps they use, having data always available, even when the device is offline has become increasingly important.
In this talk we will go through different ways of saving data on the phone and introduce Realm as a replacement for SQLite and ORM's.
Through an example app it will be demonstrated that thinking "Offline first" not only affects your apps architecture for the better, but also results in happier users.
5. Why design for offline?
• A better USER EXPERIENCE!
• You always have something to show the user
• Reduce network requests and data transferred
• Saves battery
30. SELECT owner.name, dog.name, city.name
FROM owner
INNER JOIN dog ON owner.dog_id = dog.id
INNER JOIN city ON owner.city_id = city.id
WHERE owner.name = 'Frank'
SQLite
38. x
z
y
x y z
SELECT table1.x, table2.y, table3.z
FROM table1
INNER JOIN table2 ON table1.table2_id = table1.id
INNER JOIN table3 ON table1.table3_id = table3.id
References in SQL
39. A
B C
D E F
G
Realm.getA().getC().getF()
Object Store references
40. Zero-copy
Person
{
• name
=
Tommy
• age
=
8
• dog
=
{
• name
=
Lassie
}
}
Person
{
• name
=
Tommy
• age
=
8
• dog
=
{
• name
=
Lassie
}
}
Person
{
• name
=
Tommy
• age
=
8
• dog
=
{
• name
=
Lassie
}
}
PersonProxy
{
• name
• age
• dog
}
PersonProxy
{
• name
• age
• dog
}
Person
{
• name
=
Tommy
• age
=
8
• dog
=
{
• name
=
Lassie
}
}
Realm
ORM Realm
SQLite
42. SQLite vs. Realm
• Part of Android
• Relational data model
• Based on SQL
• Public Domain
• One of the most tested pieces of software
• Need to map between SQL and Java
objects (manually or ORM).
• Foreign collections is an unsolvable
problem.
• Complex API’s
• Objects all the way down
• Zero copy architecture
• Cross-platform
• Supports encryption out of the box
• Open source*
• Custom query language
• Will add ~2500 methods + 800 kB of native
code.
• Still in beta
45. Models and Schema
public class Person extends RealmObject {
private String name;
private int age;
private Dog dog;
private RealmList<Cat> cats;
// Autogenerated getters/setters
}
public class Cat extends RealmObject {
private String name;
// Autogenerated getters/setters
}
public class Dog extends RealmObject {
private String name;
// Autogenerated getters/setters
}
46. Getting an Realm instance
// Default configuration. Schema is automatically detected
RealmConfiguration config = new RealmConfiguration.Builder(context).build();
Realm.setDefaultConfiguration(config);
Realm realm = Realm.getDefaultInstance();
47. Create objects - Realm
// Create and set persisted objects
realm.beginTransaction();
Person person = realm.createObject(Person.class);
person.setName("Young Person");
person.setAge(14);
realm.commitTransaction();
// Copy java objects
Person person = new Person();
person.setName("Young Person");
person.setAge(14);
realm.beginTransaction();
realm.copyToRealm(person);
realm.commitTransaction();
• Extend RealmObject
• POJO: Plain Old Java Object
50. Network data
// Use Retrofit to parse objects
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(JacksonConverterFactory.create())
.baseUrl("http://api.nytimes.com/")
.build();
MyRestApi service = retrofit.create(MyRestApi.class);
final Data data = service.getData();
// Copy all data into Realm
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(data);
}
});