Skip to content

Instantly share code, notes, and snippets.

@kmkrn
Created December 31, 2017 13:05
Show Gist options
  • Save kmkrn/39e69c2ac0e8a6360ee52566049ede37 to your computer and use it in GitHub Desktop.
Save kmkrn/39e69c2ac0e8a6360ee52566049ede37 to your computer and use it in GitHub Desktop.
Realm objects instances are auto-updating
class Cat: Object {
@objc dynamic var catId: Int = 0
@objc dynamic var name: String = ""
@objc dynamic var breed: String = ""
@objc dynamic var weight: Double = 0
override static func primaryKey() -> String? {
return "catId"
}
let cat = Cat()
cat.name = "Charlie"
cat.catId = 12
cat.breed = "Scottish Fold"
cat.weight = 5.2
let realm = try! Realm()
let cats = realm.objects(Cat.self)
print(cats.count) // the output will be 0
try! realm.write {
realm.add(cat)
} //now the object is actually added to the database
print(cats.count) // outputs 1 thanks to real-time updates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment