#MDBlocal
Alexander Stigsen
Founder of Realm
LONDON
#MDBLocal
What is Realm? 1. Native on-device object
database
2. Cloud service that allows
seamless synchronization and
sharing across devices
#MDBLocal
Why do you need something like Realm?
User experience is everything on mobile. Bad experiences causes
instant drop-off.
§ Users have much higher expectations of mobile apps compared to
web
§ No latency or wait times accepted
§ Apps need to work even when offline or with spotty connectivity
#MDBLocal
The failed promise of ubiquitous connectivity
• Even in major cities connectivity is spotty
• Go to the countryside, still many places with no connectivity
• Crises or huge events often takes down connectivity
• Developing countries are the next big market, but there connectivity is
even worse, and bandwidth is expensive
How do you deal with this reality as a developer?
#MDBLocal
Developer experience is key
• A data model that fits seamlessly with the platform and programming
language
• Transparent synchronization that just works
• A conflict resolution model that works both in realtime, and when
users are offline for extended periods of time
Realm is a mobile database
#MDBLocal
Cross-platform
Operating Systems
Languages/Platforms
Swift Objective C Java .Net Node.js React Native Electron
Realm is an object database
#MDBLocal
Database types
Key-
Value
Document Object
#MDBLocal
Native objects map directly to the database
// Dog model
class Dog: Object {
@objc dynamic var name = ""
@objc dynamic var owner: Person? // Properties can be optional
}
// Person model
class Person: Object {
@objc dynamic var name = ""
@objc dynamic var birthdate = Date(timeIntervalSince1970: 1)
let dogs = List<Dog>() // links to other objects
}
#MDBLocal
Query across the object graph
// Query using a predicate string
let johnsDogs = realm.objects(Dog.self).filter(”owner.name = 'John'")
// Query using an NSPredicate
let predicate = NSPredicate(format: "color = %@ AND name BEGINSWITH %@", "tan", "B")
let tanDogs = realm.objects(Dog.self).filter(predicate)
// Sort tan dogs with names starting with "B" by name
let sortedDogs = realm.objects(Dog.self).filter("color = 'tan' AND name BEGINSWITH 'B'”)
.sorted(byKeyPath:"name")
#MDBLocal
Easy and safe multi-threading with transactions
// create new object
let person = Person(name: "Jane")
// add to realm in transaction
try! realm.write {
realm.add(person)
}
Realm objects has a special capability…
#MDBLocal
Objects and queries live-update in response to changes
// Query Realm for all dogs less than 2 years old
let puppies = realm.objects(Dog.self).filter("age < 2")
puppies.count // => 0 because no dogs have been added to the Realm yet
// Persist your data easily
try! realm.write {
let myDog = Dog(name: “Fido”, age: 1)
realm.add(myDog)
}
// Queries are updated in realtime
puppies.count // => 1
Realm is a live object database
#MDBLocal
Live reactive updates
#MDBLocal
Bind directly to components for reactive UI
// Observe Results Notifications
notificationToken = results.observe { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.tableView else { return }
switch changes {
case .initial:
tableView.reloadData()
case .update(_, let deletions, let insertions, let modifications):
// Query results have changed, so apply them to the UITableView
tableView.beginUpdates()
tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}),
with: .automatic)
tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }),
with: .automatic)
tableView.endUpdates()
case .error(let error):
}
Now imagine if these live-updates came from
remote changes?
#MDBLocal
Realm Sync
#MDBLocal
Make a synced Realm with one line change
// Create the configuration
let syncServerURL = URL(string: "realms://myinstance.cloud.realm.io/~/userRealm")!
let config = user.configuration(realmURL: syncServerURL)
// Open the remote Realm
let realm = try! Realm(configuration: config)
// Any changes made to this Realm will be synced across all devices!
• Instead of a file path, specify a URL pointing to the realm in the cloud.
• Exactly the same as a local realm, but now with background
synchronization.
#MDBLocal
Automatic conflict resolution
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.
Last update wins
If two sides has updated the same property, the value will end up as the last updated.
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.
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 by building on top of these primitives if needed.
#MDBLocal
Backend integration
3rd party
databases
API’s§ Event handling
§ Listen and respond to changes from clients
§ Pass along data to other systems or
databases
§ Connectors to 3rd party databases
§ Two-way sync to legacy databases
§ Postgres and SQL Server. MongoDB coming!
§ Integrations (via Node.js & .NET SDK)
§ Identical object interface
§ Full database capabilities
§ Apply changes to push data
§ Mobilize legacy APIs
§ An easier way to integrate legacy data with
mobile devices
§ Keep complexity in backend
#MDBLocal
Realm + MongoDB
THANK YOU

MongoDB .local London 2019: Realm: The Secret Sauce for Better Mobile Apps

  • 1.
  • 2.
    #MDBLocal What is Realm?1. Native on-device object database 2. Cloud service that allows seamless synchronization and sharing across devices
  • 3.
    #MDBLocal Why do youneed something like Realm? User experience is everything on mobile. Bad experiences causes instant drop-off. § Users have much higher expectations of mobile apps compared to web § No latency or wait times accepted § Apps need to work even when offline or with spotty connectivity
  • 4.
    #MDBLocal The failed promiseof ubiquitous connectivity • Even in major cities connectivity is spotty • Go to the countryside, still many places with no connectivity • Crises or huge events often takes down connectivity • Developing countries are the next big market, but there connectivity is even worse, and bandwidth is expensive How do you deal with this reality as a developer?
  • 5.
    #MDBLocal Developer experience iskey • A data model that fits seamlessly with the platform and programming language • Transparent synchronization that just works • A conflict resolution model that works both in realtime, and when users are offline for extended periods of time
  • 6.
    Realm is amobile database
  • 7.
  • 8.
    Realm is anobject database
  • 9.
  • 10.
    #MDBLocal Native objects mapdirectly to the database // Dog model class Dog: Object { @objc dynamic var name = "" @objc dynamic var owner: Person? // Properties can be optional } // Person model class Person: Object { @objc dynamic var name = "" @objc dynamic var birthdate = Date(timeIntervalSince1970: 1) let dogs = List<Dog>() // links to other objects }
  • 11.
    #MDBLocal Query across theobject graph // Query using a predicate string let johnsDogs = realm.objects(Dog.self).filter(”owner.name = 'John'") // Query using an NSPredicate let predicate = NSPredicate(format: "color = %@ AND name BEGINSWITH %@", "tan", "B") let tanDogs = realm.objects(Dog.self).filter(predicate) // Sort tan dogs with names starting with "B" by name let sortedDogs = realm.objects(Dog.self).filter("color = 'tan' AND name BEGINSWITH 'B'”) .sorted(byKeyPath:"name")
  • 12.
    #MDBLocal Easy and safemulti-threading with transactions // create new object let person = Person(name: "Jane") // add to realm in transaction try! realm.write { realm.add(person) }
  • 13.
    Realm objects hasa special capability…
  • 14.
    #MDBLocal Objects and querieslive-update in response to changes // Query Realm for all dogs less than 2 years old let puppies = realm.objects(Dog.self).filter("age < 2") puppies.count // => 0 because no dogs have been added to the Realm yet // Persist your data easily try! realm.write { let myDog = Dog(name: “Fido”, age: 1) realm.add(myDog) } // Queries are updated in realtime puppies.count // => 1
  • 15.
    Realm is alive object database
  • 16.
  • 17.
    #MDBLocal Bind directly tocomponents for reactive UI // Observe Results Notifications notificationToken = results.observe { [weak self] (changes: RealmCollectionChange) in guard let tableView = self?.tableView else { return } switch changes { case .initial: tableView.reloadData() case .update(_, let deletions, let insertions, let modifications): // Query results have changed, so apply them to the UITableView tableView.beginUpdates() tableView.insertRows(at: insertions.map({ IndexPath(row: $0, section: 0) }), with: .automatic) tableView.deleteRows(at: deletions.map({ IndexPath(row: $0, section: 0)}), with: .automatic) tableView.reloadRows(at: modifications.map({ IndexPath(row: $0, section: 0) }), with: .automatic) tableView.endUpdates() case .error(let error): }
  • 18.
    Now imagine ifthese live-updates came from remote changes?
  • 19.
  • 20.
    #MDBLocal Make a syncedRealm with one line change // Create the configuration let syncServerURL = URL(string: "realms://myinstance.cloud.realm.io/~/userRealm")! let config = user.configuration(realmURL: syncServerURL) // Open the remote Realm let realm = try! Realm(configuration: config) // Any changes made to this Realm will be synced across all devices! • Instead of a file path, specify a URL pointing to the realm in the cloud. • Exactly the same as a local realm, but now with background synchronization.
  • 21.
    #MDBLocal Automatic conflict resolution Deletesalways win If one side deletes an object, it will always stay deleted, even if the other side has made changes to it later on. Last update wins If two sides has updated the same property, the value will end up as the last updated. 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. 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 by building on top of these primitives if needed.
  • 22.
    #MDBLocal Backend integration 3rd party databases API’s§Event handling § Listen and respond to changes from clients § Pass along data to other systems or databases § Connectors to 3rd party databases § Two-way sync to legacy databases § Postgres and SQL Server. MongoDB coming! § Integrations (via Node.js & .NET SDK) § Identical object interface § Full database capabilities § Apply changes to push data § Mobilize legacy APIs § An easier way to integrate legacy data with mobile devices § Keep complexity in backend
  • 23.
  • 24.