I use swift3 and Realm 2.3.
And I need callback after transaction is finished.
for example, I have a code as below, how can I get call back after a realm data transaction is finished ?
DispatchQueue.main.async {
try! self.realm.write {
self.realm.add(friendInfo, update: true)
}
}
Transactions are executed synchronously. So you can just perform the code right after you execute the transaction.
It depends on why you need the callback, but there are a variety of ways Realm can provide a notification when data is changed.
The most common use-case is when you're displaying a list of items from a
Results
object. In that case, you can use Realm's change notifications feature to update specific objects:Realm object properties are also KVO-compliant, so you can also use the traditional Apple
addObserver
API to track when a specific property changes.Failing all of that, if you have a very specific use-case for being notified of when a piece of Realm data changes, you can also implement your own notifications using something like
NotificationCenter
.Please follow-up if you need any additional clarification.