Hi! I as part of migrating to Atlas from a local database I am updating the app to do write transactions off the main thread.
One issue I have is that when I do user interface driven writes, I need to be able to do that asynchronously without notifying the the NotificationToken that I am using to listen for changes. Is there a way to do this?
The following does not work and will crash as you can’t ignore notification tokens on other threads.
func writeAsync<T: ThreadConfined>(obj: T, errorHandler: @escaping ((_ error: Swift.Error) -> Void) = { _ in return }, onComplete: ((T?) -> Void)? = nil, withoutNotifying:[NotificationToken] = [], block: @escaping ((Realm, T?) -> Void)) {
let wrappedObj = ThreadSafeReference(to: obj)
let config = self.configuration
DispatchQueue(label: "background").async {
autoreleasepool {
do {
let realm = try Realm(configuration: config)
let obj = realm.resolve(wrappedObj)
try realm.write(withoutNotifying: withoutNotifying) {
block(realm, obj)
}
onComplete?(obj)
} catch {
errorHandler(error)
}
}
}
}
What would be the right way to achieve async user interface driven writes without notifying the notification token on the main thread?