aux Core Data, appréciée par des centaines de milliers de développeurs. Apprenez ce qui rend Realm spécial et comment il peut être utilisé pour créer de meilleures applications plus rapidement.
3. #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.
4. #MDBLocal
What is ? 1. Native on-device object database
2. Cloud service enabling seamless
synchronization and sharing
5. #MDBLocal
2.5 B+ app installs
100k+ developers
50k+ Github stars
Active community
Massive Developer Reach
& Active Community
6. #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
7. #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
9. #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
10. #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
11. #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
12. #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
}
14. #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")
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 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
18. #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
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()
}
22. #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
23. #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
24. #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!
25. #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
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 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