Created
December 31, 2017 13:05
-
-
Save kmkrn/39e69c2ac0e8a6360ee52566049ede37 to your computer and use it in GitHub Desktop.
Realm objects instances are auto-updating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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