Reputation: 1873
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:
If write()
is synchronous, then invalidate()
is ok
And if Async than before write invalidate will call, and realm will release but operation is running in background
Thanks
Upvotes: 7
Views: 4614
Reputation: 8138
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() }
}
Upvotes: 3
Reputation: 472
The method you write is synchronous method as you did not specify the background queue for it. Purpose of Invalidate() method
func invalidate() Description Invalidates all Objects, Results, LinkingObjects, and Lists managed by the Realm. A Realm holds a read lock on the version of the data accessed by it, so that changes made to the Realm on different threads do not modify or delete the data seen by this Realm. Calling this method releases the read lock, allowing the space used on disk to be reused by later write transactions rather than growing the file. This method should be called before performing long blocking operations on a background thread on which you previously read data from the Realm which you no longer need. All Object, Results and List instances obtained from this Realm instance on the current thread are invalidated. Objects and Arrays cannot be used. Results will become empty. The Realm itself remains valid, and a new read transaction is implicitly begun the next time data is read from the Realm. Calling this method multiple times in a row without reading any data from the Realm, or before ever reading any data from the Realm, is a no-op. This method may not be called on a read-only Realm.
Upvotes: -2