Realm
Jason Flax | Lead Engineer, Realm Cocoa
Lee Maguire | Software Engineer, Realm Cocoa
100k
developers
have built apps on Realm
40k
of the world’s most
popular apps
2bn
installs and counting
across all devices
Leading Apps are Built on
Realm:
Widely Adopted Open Source Database
Realm Database
● Works offline
● Lightweight, fully-featured
database that’s embedded on the
client
● Object-oriented data model; no
need to work with ORM
● Live Objects no need to write code
to check the client-side datastore
for changes in the user’s data
● Tight integration with SwiftUI
● Encryption is easy
Realm Database
The core concept of the Realm SDK is a
lightweight object container called a
Realm. Like a database, data in Realms
can be queried and filtered,
interconnected, and persisted. Unlike a
traditional database though, objects in
Realms are live and fully reactive.
● Represents an instance of a Realm
database container (can be a file, or in
memory)
● Data internally represented and
exposed as objects
● Relations are link lists / pointers
● ACID compliant, performant
Realm
Jason Flax | Lead Engineer, Realm Cocoa
Lee Maguire | Software Engineer, Realm Cocoa
Realm Object Classes are the Schema
import RealmSwift
class Dog: Object {
@objc dynamic var name = ""
@objc dynamic var age = 0
}
class Person: Object {
@objc dynamic var name = ""
@objc dynamic var dog: Dog?
let puppies = RealmSwift.List<Dog>()
}
Objects are Live and Auto-Updating
let myDog = Dog()
myDog.name = "Fido"
myDog.age = 1
try! realm.write {
realm.add(myDog)
}
let myPuppy = realm.objects(Dog.self).filter("age == 1").first
try! realm.write {
myPuppy!.age = 2
}
print("age of my dog: (myDog.age)") // => 2
Perform Complex Queries Across Objects
// Query using a predicate
var tanDogs = realm.objects(Dog.self)
.filter("color = 'tan' AND name BEGINSWITH 'B'")
// Query using an NSPredicate
let predicate = NSPredicate(format: "color = %@ AND name BEGINSWITH %@",
"tan", "B")
tanDogs = realm.objects(Dog.self).filter(predicate)
Listen to Object Changes to Simplify Your Architecture
let realm = try! Realm()
let myPuppy = realm.objects(Dog.self).filter("age == 1").first!
// Observe Realm Notifications
let token = myPuppy.observe { change in
// ... puppy age change will appear here
}
try! realm.write {
myPuppy.age = 3
}
// Finally
token.invalidate()
Bringing it all together
Modify Update Trigger
Events
Transactions Objects Queries Notifications
Transactions are the
only things that modify
state, and do so in a
threadsafe manner
Realm objects are
flexible, and can contain
any kind of data: objects,
lists, graphs, etc.
Queries are declarative
and are automatically
updated as the state
changes.
Any state change, be it
in queries or individual
objects, can be reacted
on to update UI or
trigger side effects.
class Dog: Object {
@objc dynamic var name = ""
@objc dynamic var age = 0
}
try! realm.write {
realm.add(myDog)
}
let puppies =
dogs.filter("age < 2")
myDog.observe { change in
viewController.updateUI()
}
What’s Next for Realm Cocoa?
● SwiftUI Enhancements with Property Wrappers
● Dictionaries, Sets, Mixed, & UUID Types
● Full-text Search
● Property Wrappers for Realm properties
Join Us!
● Give it a Try - https://docs.mongodb.com/realm/ios/install/
● Ask a Question - https://forums.realm.io/
● File an Issue - https://github.com/realm/realm-cocoa
● Connect with our Community -
https://live.mongodb.com/realm-global-community/
● Join our Team -
https://www.mongodb.com/careers/departments/engineering
● Tweet at us - https://twitter.com/realm?s=21
Thank you

iOS Dev Happy Hour Realm - Feb 2021

  • 1.
    Realm Jason Flax |Lead Engineer, Realm Cocoa Lee Maguire | Software Engineer, Realm Cocoa
  • 2.
    100k developers have built appson Realm 40k of the world’s most popular apps 2bn installs and counting across all devices Leading Apps are Built on Realm: Widely Adopted Open Source Database
  • 3.
    Realm Database ● Worksoffline ● Lightweight, fully-featured database that’s embedded on the client ● Object-oriented data model; no need to work with ORM ● Live Objects no need to write code to check the client-side datastore for changes in the user’s data ● Tight integration with SwiftUI ● Encryption is easy
  • 4.
    Realm Database The coreconcept of the Realm SDK is a lightweight object container called a Realm. Like a database, data in Realms can be queried and filtered, interconnected, and persisted. Unlike a traditional database though, objects in Realms are live and fully reactive. ● Represents an instance of a Realm database container (can be a file, or in memory) ● Data internally represented and exposed as objects ● Relations are link lists / pointers ● ACID compliant, performant
  • 5.
    Realm Jason Flax |Lead Engineer, Realm Cocoa Lee Maguire | Software Engineer, Realm Cocoa
  • 6.
    Realm Object Classesare the Schema import RealmSwift class Dog: Object { @objc dynamic var name = "" @objc dynamic var age = 0 } class Person: Object { @objc dynamic var name = "" @objc dynamic var dog: Dog? let puppies = RealmSwift.List<Dog>() }
  • 7.
    Objects are Liveand Auto-Updating let myDog = Dog() myDog.name = "Fido" myDog.age = 1 try! realm.write { realm.add(myDog) } let myPuppy = realm.objects(Dog.self).filter("age == 1").first try! realm.write { myPuppy!.age = 2 } print("age of my dog: (myDog.age)") // => 2
  • 8.
    Perform Complex QueriesAcross Objects // Query using a predicate var tanDogs = realm.objects(Dog.self) .filter("color = 'tan' AND name BEGINSWITH 'B'") // Query using an NSPredicate let predicate = NSPredicate(format: "color = %@ AND name BEGINSWITH %@", "tan", "B") tanDogs = realm.objects(Dog.self).filter(predicate)
  • 9.
    Listen to ObjectChanges to Simplify Your Architecture let realm = try! Realm() let myPuppy = realm.objects(Dog.self).filter("age == 1").first! // Observe Realm Notifications let token = myPuppy.observe { change in // ... puppy age change will appear here } try! realm.write { myPuppy.age = 3 } // Finally token.invalidate()
  • 10.
    Bringing it alltogether Modify Update Trigger Events Transactions Objects Queries Notifications Transactions are the only things that modify state, and do so in a threadsafe manner Realm objects are flexible, and can contain any kind of data: objects, lists, graphs, etc. Queries are declarative and are automatically updated as the state changes. Any state change, be it in queries or individual objects, can be reacted on to update UI or trigger side effects. class Dog: Object { @objc dynamic var name = "" @objc dynamic var age = 0 } try! realm.write { realm.add(myDog) } let puppies = dogs.filter("age < 2") myDog.observe { change in viewController.updateUI() }
  • 11.
    What’s Next forRealm Cocoa? ● SwiftUI Enhancements with Property Wrappers ● Dictionaries, Sets, Mixed, & UUID Types ● Full-text Search ● Property Wrappers for Realm properties
  • 12.
    Join Us! ● Giveit a Try - https://docs.mongodb.com/realm/ios/install/ ● Ask a Question - https://forums.realm.io/ ● File an Issue - https://github.com/realm/realm-cocoa ● Connect with our Community - https://live.mongodb.com/realm-global-community/ ● Join our Team - https://www.mongodb.com/careers/departments/engineering ● Tweet at us - https://twitter.com/realm?s=21
  • 13.