Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RealmSwift - transaction completion

Tags:

ios

swift

realm

How do I know when a particular transaction has completed?

I want to run a particular block of code after a transaction is complete. How can I do this?

I am performing writes in the following war -

do {

  try realm.write({
    realm.add(<some object>)
  })
}
catch {}
like image 403
utkbansal Avatar asked Jun 25 '16 15:06

utkbansal


1 Answers

Transactions are executed synchronously. So you can just perform the code right after you execute the transaction.

I would recommend to "force-try" as seen below to abort in case of an error if you don't provide any kind of error handling. Transactions fail recoverable if you run out of disk space. In most situations, you would expect in the code run after a transaction, that your data was persisted, which wouldn't apply in case of an error. Furthermore you don't see any errors in development if you keep the catch path completely empty.

try! realm.write {
    realm.add(<some object>)
}
runMoreCode()
like image 165
marius Avatar answered Sep 22 '22 22:09

marius