Offline login with already synced Realm



I have implemented Realm.asyncOpen and it is working fine, I’m able to receive and sync data to my server.
I just tested the scenario, when an already logged in user try to start my app without Internet connection and I got an error.

Only 1 user can access one time my app, so in the AppDelegate I check for SyncUser.all. There is the saved user, so I try to async open a realm with this stored user. I got error logs (Failed to connect to endpoint ‘’: Connection refused) - thats great, because the server is unreachable without internet.

The problem is that the Realm.asyncOpen call doesnt return anything in this scenario.
The “let realm = try! Realm(configuration: config)” returns, but i got an empty realm (realm.isEmpty = true).

Am i wrong? How can i open a synced realm offline?


For anyone who interested:

I managed making it working with a workaround.
On first login or registration i’m using the Realm.openAsync method, but if the user already logged in then i’m using the “let realm = try! Realm(configuration: config)” way.

I will test it in the next few days, but it seems to be working right now.


Perhaps you can help me - I just stumbled on the same problem.

The usual steps I use are to login the user and then use the returned SyncUser.configuration() to open the synced realm.

But if the internet is not available then do I just ignore the user.configuration() and use something else ?


This is definitely possible to do with the beta RMP. I am just in the process of upgrading to Realm Cloud and encountered this issue. You could definitely open a synced realm offline and when the data connection is restored the data syncs.

Realm is offline first! In this situation basically the app is literally unusable if not connected to the internet. It makes no sense. I am sure realm doesn’t want us to manage an offline state. And I know this used to work fine in the beta.


HI freeubi,

I am stuck in the area where u stucked. Can you explain your workaround ?


of course.

The documentation is not too clear about this area, but basically we have 2 option to create a realm.

  1. async [aka. Realm.openAsync…]: with this, you wait for the data download. This is the scenario on registration, and first login. If the server, or the user is offline, then this will never* return -> because you wait for the data and it cant check for it.
    *not really never, it will timeout after some time :slight_smile:

  2. sync [aka.let realm = try! Realm(configuration: config)]: this call will check for the realm file, and if its not created yet, then it will be created. If its available, then you can use it right away.
    If you use syncConfiguration, then it will download all the data later, but it wont be available after this call returns.
    This scenario is good for a normal login.

I build my apps offline first - so it doesn’t matter, if the new data is not on the phone already after the login.
If you must have all the data, then i suggest to check if the user if online on application start, and then use the correct method from above :slight_smile:

Is it clearer now? Ping me if you have any question :slight_smile: