Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Realm Write method is Sync or Async Thread

Tags:

ios

swift

realm

I am using Realm in My project, and I want to know whether the realm.write() method is synchronous or not. My example is here:

 let realm = try! Realm()
    try! realm.write {
        realm.delete(message)
    }
    realm.invalidate()

In the above example, I am deleting a realm object and outside braces I am writing invalidate()

Here is my confusion:

  1. If write() is synchronous, then invalidate() is ok

  2. And if Async than before write invalidate will call, and realm will release but operation is running in background

Thanks

like image 460
Pradeep Bishnoi Avatar asked Jan 13 '17 08:01

Pradeep Bishnoi


Video Answer


1 Answers

Realm.write is synchronous. It just calls realm.beginWrite()/realm.commitWrite() with some error handling:

public func write(_ block: (() throws -> Void)) throws {
    beginWrite()
    do {
        try block()
    } catch let error {
        if isInWriteTransaction { cancelWrite() }
        throw error
    }
    if isInWriteTransaction { try commitWrite() }
}
like image 141
Thomas Goyne Avatar answered Oct 03 '22 10:10

Thomas Goyne