Realm.asyncOpen Works 9/10 times

Hello I am developing a swift application for IOS and using a MongoDB Realm database. I am having problems with Realm.asyncOpen(configuration: <#T##Realm.Configuration#>, callbackQueue: <#T##DispatchQueue#>, callback: <#T##(Result<Realm, Error>) -> Void#>) as it only works 90% of times (Around that percentage). I have tried the other variations of Realm.async() to see if it was just that function but it is not. I am using this function in my SplashScreen (First View Controller) in order to grab the realm and any changes done to it from my MongoDB Cluster as it using Realm Sync. Problem is that sometimes when it is called there is no callback being given, no result or fail, it’s just in an indefinite loop which lasts from a minute to ten minutes (normally takes couple seconds). This causes my animation screen to run indefinitely as I am waiting for the callback. Once this loop happens the next several runs are fine, but then it comes back again.

This is the printed console when the indefinite loop occurs:
User is logged in

2021-02-16 17:13:51.964779-0500 PlantersHandbook[2968:968859] Sync: Connection[1]: Session[1]: client_reset_config = false, Realm exists = true, async open = false, client reset = false

2021-02-16 17:13:52.051113-0500 PlantersHandbook[2968:968859] Sync: Connection[1]: Connected to endpoint ‘’ (from ‘’)

Note: Code does not get into the callback function when in the indefinite loop (1-10minutes)

override func viewDidAppear(_ animated: Bool) {
    self.navigationController?.navigationBar.isHidden = true

fileprivate func checkingConfiguration(){
    if let _ = app.currentUser{
        print("User is logged in")
        var configuration = app.currentUser!.configuration(partitionValue: "user=\(app.currentUser!.id)", cancelAsyncOpenOnNonFatalErrors: true)
        configuration.objectTypes = [User.self, Season.self, HandbookEntry.self, Block.self, SubBlock.self, Cache.self, BagUpInput.self, PlotInput.self, CoordinateInput.self, Coordinate.self, ExtraCash.self]
        Realm.asyncOpen(configuration: configuration, callbackQueue: .main, callback: { result in
            switch result {
            case .failure( _):
                print("Failed to load aysnc")
                if Realm.fileExists(for: configuration){
                    self.enterapp(configuration: configuration, foundRealm: nil)
                    print("User not logged in; present sign in/sign up view")
                    self.navigationController?.pushViewController(WelcomeViewController(), animated: false)
            case .success(let realm):
                self.enterapp(configuration: configuration, foundRealm: realm)
    }else {
        print("User not logged in; present sign in/sign up view")
        self.navigationController?.pushViewController(WelcomeViewController(), animated: false)

My Questions
Does anyone have similar problems? Can I set a timer somewhere to break the loop that is stuck and restart the Realm.asyncOpen somehow?

Set Up
Xcode 12.4
IOS Deployment Target - 14.3
Updated Cocoa Pods for Realm and RealmSwift

It’s a good idea to include code as text in your question. That way, it we want to test it or use in an answer we don’t have to retype it.

I would suggest a couple of things to do to begin troubleshooting - try these suggestions and see if it makes a difference.

Call checkingConfiguration outside of the DispatchQueue (e.g. comment out the DispatchQueue code).

Also, change your Realm.asyncOpen to this

Realm.asyncOpen(configuration: config) { result in

lastly, comment out everything within that closure and leave it as

case .success(let realm):
   print("Successfully opened realm: \(realm)")

Thanks for your response Jason, I have changed the picture of the original code to text and tried your solutions but sadly I am still getting the same result.

Im going to try to code a way that if the Realm.async lasts over 15 seconds, to check if the realm file exists on the device and open the realm normally, not communicating with the remote database first. If there is no file on the device then the user will just be sent to the welcome screen.

I copy and pasted your code and it is working correctly.

That would make me think it’s something else - maybe a firewall or network issue or some other code causing the delays.

@Jay did you run it a bunch of times and it worked 100%? Did you have a logo animation running or just the normal start up? If so it could be because the logo animation is occupying something that blocks the callback, or it could be what you said about some firewall or network issue.

If it is the firewall or network issue, I think the best bet would still be to have some sort of timer that just pulls the local realm after a decent time of trying to grab from the remote database and use it instead.

I do not have that in my project because that code wasn’t specified in the question. But yeah - if you’re doing something in the main thread it could be gumming up the works.

I ran it about 15 times over the course of two hours with no issues. Once the code was whittled down it’s kinda standard connection code that closely matches the code in the getting started guide so I would expect it to work.

Comment out that animation call and see what happens. You could also add a breakpoint and see specifically what line it causing the delay.

I tried with with and without cancelAsyncOpenOnNonFatalErrors as well.

@Jay I took out the logo animation and the loop is still happening. It only happens when I am connected to internet (wifi or cellular), so it must be an issue with the connection to my realm remote database.

Thanks for all the help so far.