#MDBlocal
Sergi Vives
Solutions Architect
@sergivives
PARIS
……..<< …….
#MDBLocal
About me…
Solutions Architect for MongoDB in
Barcelona.
@sergivives
sergi.vives@mongodb.com
My Background:
BSc Computer Science, Barcelona
MSc Business Systems Design, London
Developer: +10 years
Freelance iOS Developer: 3+ yrs.
#MDBLocal
What is ? 1. Native on-device object database
2. Cloud service enabling seamless
synchronization and sharing
#MDBLocal
2.5 B+ app installs
100k+ developers
50k+ Github stars
Active community
Massive Developer Reach
& Active Community
#MDBLocal
Why do you need something like Realm?
User experience is everything on mobile.
Bad experience = instant drop-off.
§ Much higher expectations of mobile apps vs the web
§ No latency or wait times accepted
§ Apps need to work when offline or with bad connectivity
#MDBLocal
The failed promise of ubiquitous connectivity
• Connectivity is spotty even in major cities
• The countryside, many places with no connectivity
• Crises or huge events often takes down signal
• Developing countries, connectivity is bad and bandwidth is expensive
Painful to deal with this as a developer
#MDBLocal
We’ve assumed that REST is the answer…
#MDBLocal
Developer experience is key
1. A data model that fits seamlessly with the platform and code
2. Transparent synchronization that just works
3. A conflict resolution model for both in real-time & offline
#MDBLocal
Small assignment: TODO App
• List of projects (stream of things to do)
• Each Project has tasks (items)
• Tech details:
• In Swift on iPhone, iPad and MacOS
• Needs to work ALWAYS
• offline
• with bad signal
• Deadline: Yesterday
#MDBLocal
Cross-platform
1. A data model that fits seamlessly with the platform and code
Operating Systems
Languages/Platforms
Swift Objective C Java .Net Node.js React Native Electron
#MDBLocal
Native objects map directly to the database
1. A data model that fits seamlessly with the platform and code
import RealmSwift
// Task model
class Task: Object {
@objc dynamic var body: String = "”
@objc dynamic var isDone: Bool = false
@objc dynamic var due = Date()
@objc dynamic var owner: User
}
// Project model
class Project: Object {
@objc dynamic var name = ""
@objc dynamic var timestamp = Date()
let tasks = List<Task>() // links to other objects
}
#MDBLocal
Database types
1. A data model that fits seamlessly with the platform and code
Key-Value Document Object
#MDBLocal
Query across the object graph
1. A data model that fits seamlessly with the platform and code
// Query using a predicate string, George’s tasks
let georgeTasks = realm.objects(Task.self).filter("task.owner.name = ‘George'")
// Search for George’s tasks that start with Buy, and sort them by date
let sortedtasks =
realm.objects(Task.self)
.filter("task.owner.name = ‘George' AND name BEGINSWITH ‘Acheter’")
.sorted(byKeyPath:"timestamp")
#MDBLocal
● Comparisons
○ =, ==
○ >=, =>
○ <=. =<
○ <
○ >
○ !=, <>
○ IN
○ BETWEEN
● String Comparisons
○ BEGINSWITH
○ CONTAINS
○ ENDSWITH
○ LIKE
● Compound Operators
○ AND, &&
○ OR, ||
○ NOT, !
● Collection queries
○ ANY, SOME
○ ALL
○ NONE
○ IN
● Aggregates
○ count
○ avg
○ min
○ max
○ sum
● Subqueries
○ SUBQUERY()
Queries can be complex
#MDBLocal
Developer experience is key
1. A data model that fits seamlessly with the platform and code
2. Transparent synchronization that just works
3. A conflict resolution model for both in real-time & offline
Realm objects have a special capability…
#MDBLocal
Objects and queries live-update in response to changes
// Query Realm for all recent tasks, realm is empty at this point
let tasks_due = realm.objects(Tasks.self).filter(”due < ‘2020-01-31’")
tasks_due.count // => 0 because no tasks have been added
try! realm.write {
let motoTask = Task(body: “Take motorbike to review”, due: “2020-01-25”)
realm.add(motoTask)
}
// Queries are updated in realtime
tasks_due.count // => 1
#MDBLocal
Live reactive updates
try! realm.write {
realm.add(motoTask)
}
Class Task: Object {
dynamic var name = “”;
dynamic var due = Date();
}
let due_tasks =
tasks.filter(
“due < today”
)
realm.addNotificationBlock
{ notification,
realm in
viewController.updateUI()
}
Now,
imagine if these live-updates
came from remote changes?
#MDBLocal
MongoDB + Realm
Realtime
data sync
#MDBLocal
Realtime
data sync
● On-device object database
● Offline-first API
● Native support for relationships
● Change notifications keep UI up-to-date
● Abstracts network handling
● Removes (de)serialization
● Built-in encryption
Mobile databaseMongoDB + Realm
#MDBLocal
MongoDB + Realm
Realtime
data sync
● Realtime sync
● Automatic conflict resolution
● Flexible schema change support
● Event handling
● Integrate existing infrastructure
● Supports data push
● Atlas as the persistence layer
Realm Server on Atlas
#MDBLocal
Make a synced Realm with one line change
// Create the configuration
let syncServerURL = URL(string: "realms://myinstance.mongodb.com/~/userRealm")!
let config = user.configuration(realmURL: syncServerURL)
// Open the remote Realm
let realm = try! Realm(configuration: config)
// NOW, Any changes made to this Realm will be synced across all devices!
#MDBLocal
Developer experience is key
1. A data model that fits seamlessly with the platform and code
2. Transparent synchronization that just works
3. A conflict resolution model for both in real-time & offline
But,
in life,
there is always a bit of
conflict
#MDBLocal
Automatic conflict resolution
3. A conflict resolution model for both in real-time & offline
1. Deletes always win
If one side deletes an object, it will always stay deleted, even if the other side has made changes to it later on.
2. Last update wins
If two sides has updated the same property, the value will end up as the last updated.
3. Inserts in lists are ordered by time
If two items are inserted at the same position, the item that was inserted first will end up before the other
item.
4. Primary keys define object identity
If two sides both creates objects with same primary key, they will be merged into a single object.
Four simple rules
for deterministic
realtime conflict
resolution,
customizable if
needed.
#MDBLocal
Developer experience is key
1. A data model that fits seamlessly with the platform and code
2. Transparent synchronization that just works
3. A conflict resolution model for both in real-time & offline
DEMO
TODO App
#MDBLocal
Summary: Realm & MongoDB
Offline-fist on-device object database
Cross-platform, native
No boilerplate sync code
Secure and encrypted
Integrated with MongoDB Atlas
……..<< …….
THANK YOU

MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures applications mobiles

  • 1.
  • 2.
  • 3.
    #MDBLocal About me… Solutions Architectfor MongoDB in Barcelona. @sergivives sergi.vives@mongodb.com My Background: BSc Computer Science, Barcelona MSc Business Systems Design, London Developer: +10 years Freelance iOS Developer: 3+ yrs.
  • 4.
    #MDBLocal What is ?1. Native on-device object database 2. Cloud service enabling seamless synchronization and sharing
  • 5.
    #MDBLocal 2.5 B+ appinstalls 100k+ developers 50k+ Github stars Active community Massive Developer Reach & Active Community
  • 6.
    #MDBLocal Why do youneed something like Realm? User experience is everything on mobile. Bad experience = instant drop-off. § Much higher expectations of mobile apps vs the web § No latency or wait times accepted § Apps need to work when offline or with bad connectivity
  • 7.
    #MDBLocal The failed promiseof ubiquitous connectivity • Connectivity is spotty even in major cities • The countryside, many places with no connectivity • Crises or huge events often takes down signal • Developing countries, connectivity is bad and bandwidth is expensive Painful to deal with this as a developer
  • 8.
    #MDBLocal We’ve assumed thatREST is the answer…
  • 9.
    #MDBLocal Developer experience iskey 1. A data model that fits seamlessly with the platform and code 2. Transparent synchronization that just works 3. A conflict resolution model for both in real-time & offline
  • 10.
    #MDBLocal Small assignment: TODOApp • List of projects (stream of things to do) • Each Project has tasks (items) • Tech details: • In Swift on iPhone, iPad and MacOS • Needs to work ALWAYS • offline • with bad signal • Deadline: Yesterday
  • 11.
    #MDBLocal Cross-platform 1. A datamodel that fits seamlessly with the platform and code Operating Systems Languages/Platforms Swift Objective C Java .Net Node.js React Native Electron
  • 12.
    #MDBLocal Native objects mapdirectly to the database 1. A data model that fits seamlessly with the platform and code import RealmSwift // Task model class Task: Object { @objc dynamic var body: String = "” @objc dynamic var isDone: Bool = false @objc dynamic var due = Date() @objc dynamic var owner: User } // Project model class Project: Object { @objc dynamic var name = "" @objc dynamic var timestamp = Date() let tasks = List<Task>() // links to other objects }
  • 13.
    #MDBLocal Database types 1. Adata model that fits seamlessly with the platform and code Key-Value Document Object
  • 14.
    #MDBLocal Query across theobject graph 1. A data model that fits seamlessly with the platform and code // Query using a predicate string, George’s tasks let georgeTasks = realm.objects(Task.self).filter("task.owner.name = ‘George'") // Search for George’s tasks that start with Buy, and sort them by date let sortedtasks = realm.objects(Task.self) .filter("task.owner.name = ‘George' AND name BEGINSWITH ‘Acheter’") .sorted(byKeyPath:"timestamp")
  • 15.
    #MDBLocal ● Comparisons ○ =,== ○ >=, => ○ <=. =< ○ < ○ > ○ !=, <> ○ IN ○ BETWEEN ● String Comparisons ○ BEGINSWITH ○ CONTAINS ○ ENDSWITH ○ LIKE ● Compound Operators ○ AND, && ○ OR, || ○ NOT, ! ● Collection queries ○ ANY, SOME ○ ALL ○ NONE ○ IN ● Aggregates ○ count ○ avg ○ min ○ max ○ sum ● Subqueries ○ SUBQUERY() Queries can be complex
  • 16.
    #MDBLocal Developer experience iskey 1. A data model that fits seamlessly with the platform and code 2. Transparent synchronization that just works 3. A conflict resolution model for both in real-time & offline
  • 17.
    Realm objects havea special capability…
  • 18.
    #MDBLocal Objects and querieslive-update in response to changes // Query Realm for all recent tasks, realm is empty at this point let tasks_due = realm.objects(Tasks.self).filter(”due < ‘2020-01-31’") tasks_due.count // => 0 because no tasks have been added try! realm.write { let motoTask = Task(body: “Take motorbike to review”, due: “2020-01-25”) realm.add(motoTask) } // Queries are updated in realtime tasks_due.count // => 1
  • 19.
    #MDBLocal Live reactive updates try!realm.write { realm.add(motoTask) } Class Task: Object { dynamic var name = “”; dynamic var due = Date(); } let due_tasks = tasks.filter( “due < today” ) realm.addNotificationBlock { notification, realm in viewController.updateUI() }
  • 20.
    Now, imagine if theselive-updates came from remote changes?
  • 21.
  • 22.
    #MDBLocal Realtime data sync ● On-deviceobject database ● Offline-first API ● Native support for relationships ● Change notifications keep UI up-to-date ● Abstracts network handling ● Removes (de)serialization ● Built-in encryption Mobile databaseMongoDB + Realm
  • 23.
    #MDBLocal MongoDB + Realm Realtime datasync ● Realtime sync ● Automatic conflict resolution ● Flexible schema change support ● Event handling ● Integrate existing infrastructure ● Supports data push ● Atlas as the persistence layer Realm Server on Atlas
  • 24.
    #MDBLocal Make a syncedRealm with one line change // Create the configuration let syncServerURL = URL(string: "realms://myinstance.mongodb.com/~/userRealm")! let config = user.configuration(realmURL: syncServerURL) // Open the remote Realm let realm = try! Realm(configuration: config) // NOW, Any changes made to this Realm will be synced across all devices!
  • 25.
    #MDBLocal Developer experience iskey 1. A data model that fits seamlessly with the platform and code 2. Transparent synchronization that just works 3. A conflict resolution model for both in real-time & offline
  • 26.
    But, in life, there isalways a bit of conflict
  • 27.
    #MDBLocal Automatic conflict resolution 3.A conflict resolution model for both in real-time & offline 1. Deletes always win If one side deletes an object, it will always stay deleted, even if the other side has made changes to it later on. 2. Last update wins If two sides has updated the same property, the value will end up as the last updated. 3. Inserts in lists are ordered by time If two items are inserted at the same position, the item that was inserted first will end up before the other item. 4. Primary keys define object identity If two sides both creates objects with same primary key, they will be merged into a single object. Four simple rules for deterministic realtime conflict resolution, customizable if needed.
  • 28.
    #MDBLocal Developer experience iskey 1. A data model that fits seamlessly with the platform and code 2. Transparent synchronization that just works 3. A conflict resolution model for both in real-time & offline
  • 29.
  • 30.
    #MDBLocal Summary: Realm &MongoDB Offline-fist on-device object database Cross-platform, native No boilerplate sync code Secure and encrypted Integrated with MongoDB Atlas
  • 31.
  • 32.