Consider the following example:
class Item: Object {
@objc dynamic var id: UUID = UUID()
@objc dynamic var value: Int = 0
override class func primaryKey() -> String? {
return "id"
}
}
let realm = try! Realm()
try! realm.write {
let query1 = realm.objects(Item.self)
precondition(query1.isEmpty)
let newItem = Item()
realm.add(newItem)
precondition(query1.contains(newItem))
let query2 = realm.objects(Item.self).filter("value == 12")
precondition(query2.isEmpty)
newItem.value = 12
precondition(query2.count == 1 && query2[0] == newItem)
}
When running this program, I can observe that Realm does in fact live-update queries, and all of the preconditions pass.
My question is the following: is this behaviour guaranteed? I.e., does Realm guarantee that accessing a query will always return the current in-memory version of objects of that query's predicate (as long as you're on the same thread that created the query), even for changes that have not yet been committed to disk (by ending the write transaction)?
Note: I am asking only about the behaviour when manually accessing a query object (e.g., by checking its count or whether it contains some object, as done in the example above). I am NOT asking about the behaviour of observers, which obviously won't get called until the write transaction is actually committed.