Getting blocked on main thread


Hi there,
It seems my main thread is getting blocked when initializing Realm. This is my code:

var config = user.configuration(realmURL: syncUserRealmURL,
                                fullSynchronization: true,
                                enableSSLValidation: false,
                                urlPrefix: nil)
config.objectTypes = [....]
config.schemaVersion = 1

realm = try! Realm(configuration: config) <<< this call blocks the main thread for a few seconds

Any ideas?


Of course, its blocked.
Try out the async method, that won’t block any thread :wink:


Thanks! But I must be missing something. Because this function (and probably the non-async too) only works when I’m connected to the internet.

When not connected, the async actually fails and never calls the completion block. Obviously realm is offline first, so how do I enable that?


I don’t think that you can open a synced realm, without actually downloading the data from the server. The registration also needs internet, you open realm after that.

Without sync, of course you can open realms without internet, so thats can be a solution - if the user has internet then open a sync realm, if not then open a normal realm. If the user has internet then open the synced realm and copy all of the normal realms data.

This was my other fix:

Also, this thread can be helpfull too: