Realm.asyncOpen never resolves with realm or error


#1

Things to know before reading:

  1. I am testing when there is no network connection.
  2. I am using PromiseKit 6.
  3. I have a UIActivityIndicatorView showing while attempting to open the realm.

Summary of problem:

When testing with no network connection, I would expect that the call to Realm.asyncOpen
would execute the “else if let error = error” due to the fact that there is no way I could make contact with the server with no network connection. However, the app just hangs and no code ever executes within the asyncOpen closure.

Any advice on how this should be handled would be super appreciated. I want to avoid a scenario where all the user sees is a never ending UIActivityIndicatorView on screen.

 private func openRealm(user: SyncUser) -> Promise<Realm> {
        return Promise { seal in
            let config = user.configuration(realmURL: Constants.db)
            Realm.asyncOpen(configuration: config) { (realm, error) in
                if let realm = realm {
                    seal.fulfill(realm)
                } else if let error = error {
                    seal.reject(error)
                }
            }
        }
    }

#2

Unfortunately asyncOpen wont return in this case.
Are you sure that you need to asyncOpen every login? Can the user have new data in the cloud when they login?


#3

Hi @freeubi,

Thanks for the response. In my application, I have three different user types:

  1. Music Fan
  2. Artist
  3. Admins

The reason I am using asyncOpen on each app launch is to access a custom object within my reference realm called “User” which has a property “User Type.” I then identify the user type and route the user to the appropriate view controller.

Do you see any better solution? I’m all for suggestions!


#4

I am used a different method.
1.) on login, i am using async open too. I think its a reasonable limit to have internet for a login :slight_smile:
2.) after login, i have “auto login” feature - so if a user closes the app, i am logging back them automatically. For this purpose i wont use asyncOpen, just a normal open realm.
3.) registration is strange, i am checking for existing user so i need internet, but i have a fallback option - if the user don’t have internet, i put up a popup saying that if you previously created an account then please enable internet, or you can continue with a new account.

As far as i know, the asyncOpen is used times where you need to download data from cloud, if your local data is fine then you can have normal open.


#5

@freeubi that makes sense! It sounds like we are following similar user flows in terms of auto login. I agree that a reasonable limit is having internet. I think I’m going to test out putting a time out around the asyncOpen and if the timeout is reached, I’ll show a pop up as you’re doing.

Thanks for the replies!