real-time collaborative apps with realm... · 2017-03-22 · apps with realm nikola irinchev....

22
Real-Time Collaborative Apps with Realm Nikola Irinchev

Upload: others

Post on 01-Jun-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Real-Time Collaborative Apps with RealmNikola Irinchev

Page 2: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Agenda• Intro to the Realm Mobile Database• Getting started with the Realm Mobile Platform• [Demo] Deploying on Azure• [Demo] Implementing a messaging app

Page 3: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Realm Mobile Database• On-device cross-platform object database• Launched July 2014• Developed in the open, fully open source• 19K+ GitHub stars, 100K+ active developers

Page 4: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

How does it differ from SQLite?

Page 5: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Realm is an object database• Realm is built from scratch and optimized specifically

for mobile

• There is no translation layer - the objects are the database

• Realm is fast and therefore easy on the battery.

Page 6: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

How do I use it?

Page 7: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Models public class Dog : RealmObject { public string Name { get; set; } public int Age { get; set; } }

Page 8: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Use LINQ to perform complex queries across your object graph

// Fluent or Extension Syntaxvar oldDogs = realm.All<Dog>().Where(d => d.Age > 8);

// RealmResult collection conforms to the IQueryableforeach (var d in oldDogs) { Debug.WriteLine(d.Name);}

Page 9: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Mutate data in transactionsrealm.Write(() =>{ realm.Add(new Dog { Name = "Rex", Age = 1 }); realm.Add(new Dog { Name = "Fido", Age = 2 });});

Page 10: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Relations work like they shouldpublic class Dog : RealmObject{ public string Name { get; set; } public int Age { get; set; }} public class Person : RealmObject{ public string Name { get; set; } public IList<Dog> Dogs { get; }}

Notice that there is no ID property on Dog

Page 11: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Liveness and reactivenessObjects and queries represent the current state of the database. You don't need to manually refresh.

Page 12: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Realm objects live-update in response to changes

var puppies = realm.All<Dog>().Where(d => d.Age < 2);

puppies.Count(); // => 0 because no dogs have been added yet

// Update and persist objects with a transactionrealm.Write(() => { realm.Add(new Dog { Name = "Rex", Age = 1 });});

// Queries are updated in realtimepuppies.Count(); // => 1

Page 13: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

DatabindingAll RealmObject's implement INotifyPropertyChanged.Lists and query results implement INotifyCollectionChanged.

These work cross-thread and cross processes.

Page 14: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

TwoWay databindingAll RealmObject's implement IReflectableType to handle properties being set by the binding engine

Page 15: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Threadingpublic class Dog : RealmObject{ [PrimaryKey] public string Id { get; set; } = Guid.NewGuid().ToString(); public string Name { get; set; } public int Age { get; set; }} string id = null; realm.Write(() =>{ var dog = realm.Add(new Dog { Name = "Rex", Age = 1 }); id = dog.Id;});Task.Run(() =>{ var realm = Realm.GetInstance(); var dog = realm.Find<Dog>(id); // compute something really expensive here});

Page 16: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Realm Mobile Platform

Page 17: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Realm Mobile

Database

Real-time Sync

Realm Object Server

SDKSDKSDK

Sync Engine Dashboard Auth

System

Encryption Layer

What is it?

Access Control Full DB access

Data connectorsObject Store

S

SDK

SDK

EnterpriseFeature

Event Framework

Page 18: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Eliminate Network Challenges

SDK1 2

• Offline-first• Full database on the device

• Queries run locally• Writes apply immediately

• Reactive architecture keeps UI up-to-date• Automatic sync happens in the background• Resilient to any network conditions

Page 19: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Open your realm with sync

var credentials = Credentials.UsernamePassword(username, password, false);var authURL = new Uri("http://my.realm-auth-server.com:9080");var user = await User.LoginAsync(credentials, authURL);var serverURL = new Uri("realm://my.realm-server.com/~/default"); var configuration = new SyncConfiguration(user, serverURL);var realm = Realm.GetInstance(syncConfiguration);

Page 20: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Demo

Page 21: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Tips and key takeaways• Use multiple realms in an app• Make the most of databinding support• Back the smallest shareable unit with a

single realm

Page 22: Real-Time Collaborative Apps with Realm... · 2017-03-22 · Apps with Realm Nikola Irinchev. Agenda • Intro to the Realm Mobile Database • Getting started with the Realm Mobile

Questions?

Nikola [email protected]

@nirinchev