Realm Sync - synchronous GetInstance slows down the UI when offline

I have an application that has a service class called RealmService that gets a realm instance as follows:

public async Task<Realm> GetInstanceAsync()
    {
        try
        {
            SyncConfiguration config = new SyncConfiguration("Scribetech", _realmUser);
            return Connectivity.NetworkAccess == NetworkAccess.Internet ? await Realm.GetInstanceAsync(config) : Realm.GetInstance(config);
        }
    }

I use this service throughout the application every time I get data from the realm. I have the conditional expression in the method because GetInstanceAsync doesn’t work when the application is offline.

When I sign in online it works fine, however, when I sign in offline, the UI lags massively and is barely usable at all. I imagine it’s something to do with the GetInstance method not behaving exactly right when using a SyncConfiguration.

I use this method just by awaiting it in other classes and then accessing the realm object, usually in a using statement to prevent too many realms being open at once. Some of the realms have to stay open though, for example the SubscribeForNotifications listeners.

Any guidance on avoiding this lag and implementing the method properly would be massively appreciated.

I’ll answer to this question in your other forum post.

1 Like