Does the Realm(configuration: rc, queue: rq) queue parameter work like I hope?

thisRealm is the synchronized MongoDB I got from asyncOpen.

let rc = thisRealm.configuration
let rq = DispatchQueue(label: “readQueue”, qos: .userInteractive, autoreleaseFrequency: .workItem)
let readRealm: RealmSwift.Realm // not defined yet.
rq.sync {
readRealm = try! Realm(configuration: rc, queue: rq)
}

In the Xcode quick help

Parameters

configuration

A configuration value to use when creating the Realm.

queue

An optional dispatch queue to confine the Realm to. If given, this Realm instance can be used from within blocks dispatched to the given queue rather than on the current thread.

I’m confused:

  • On one hand, the documentation in this Quick Help seems to indicate that I can create one realm and use it in any block dispatched to that queue.
  • On the other hand, according to Apple, serial dispatch queues are not guaranteed to use the same thread each time they are used.

Am I tempting Realm fatalErrors if I try to use readRealm in multiple serial dispatch blocks? If so, what is the point of the initializer with the queue parameter?

May I ask what the intended use case is? (why are you asking?)

I think if we have some clarity on what you’re attempting to do we can provide a more accurate answer or propose some alternatives.

I was hoping to be able to use:

rq.sync {
    readrealm.write {
        ....
    }
}

within some other async code blocks. Avoiding having to use

try! Realm(configuration: rc)

within every DispatchQueue block.

However, I just found this in the release notes from 10.19.0

  • Async Realm init would often give a Realm instance which could not actually be used and would throw incorrect thread exceptions. It now is @MainActor and gives a Realm instance which always works on the main actor. The non-functional queue: parameter has been removed (since v10.15.0).

Looks like I should just ignore the Realm(configuration: rc, queue: rq) initilalizer, it’s been deprecated.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.