Randy Randy - 5 months ago 61
Swift Question

Calling completion block once objects are written in Realm

I'm using

Realm
and I love it so far but now I'm trying to find a way to know when objects are written in Realm.
Let's say I remove objects from Realm :

func deleteObjects<T: Object>(objects: Results<T>)
{
let realm = Realm()
try! realm.write {
realm.delete(objects)
}
}


Is there some existing method that would allow me to know when these objects are deleted from the database ? Something like :

func deleteObjects<T: Object>(objects: Results<T>)
{
let realm = Realm()
try! realm.write {
realm.delete(objects, completion {
print("Objects deleted")
})
}
}

Answer

Realm.delete(_:) is a synchronous operation, so the objects have been deleted when the call returns. The deletion becomes visible to other threads after the write transaction is committed (i.e., the block you pass to write completes) and their Realms refresh to pick up the changes (either via an explicit call to Realm.refresh, or via auto-refresh happening on the next runloop iteration).