Realm Swift Tutorial failing offline-first usage

The docs team is working on improving the guidance around asyncOpen() to be less “You must use asyncOpen()” and more “Here’s when you want to use asyncOpen()” with a hopefully clear message that it’s fine not to use it the rest of the time.

WRT to forcing sync to reconnect, you can call realm.syncSession.suspend(); realm.syncSession.resume(), although that doesn’t do exactly what you’d want (as it forces a disconnect even if it’s already connected). I don’t think there’s any reason for us not to expose a reconnect() function as the Java SDK does; our goal is to not need it and just do a good enough job automatically but it’s not hard to imagine scenarios where it’s useful (e.g. if the device has a very flakey connection we usually don’t want to kill the battery spamming connection attempts, unless the user is mashing a refresh button because they really do need the data ASAP).

3 Likes

Actually there is one thing you want to watch out for when using Realm() to open the first time.

If you change the schema then the call to Realm() will block while any migration is run and if you are doing this on the main thread for the first time then your app may become unresponsive if the migration takes a long time to run.

I just realised we make our first call when the app is restarted but after it has already been initialised using DispatchQueue.global().async{} with a callback and once this returns we open the main thread realm and retain that for the life of the application. That way we avoid blocking the main thread.

Being a macOS app we get the umbrella of death if there is a schema change that requires a migration. Bear in mind changing from SDK v4 to v5 or v10 causes a migration as well even if no schema changes.

Hope this helps.

1 Like

Hello @Sebastian_Dwornik ,

I’m late to this thread , like you am coming from a project that we tried Firestore with but found it wanting when offline.

Did you finally get your app working off-line as required/expected ?

@John_Kan

Welcome to the forums. This is fairly old thread.

Keep in mind that Firestore is not an offline database, it’s Online First with offline persistence for short internet outages. Realm is a very different platform with Offline First being the priority.

If you have a specific question about coding or the platform, I would suggest searching the forums or StackOverflow first, and if you don’t find a suitable answer, create a new thread so we can take a look!

Hi @Jay , hey thanks for replying,

Yes, so We discovered after going a fair way down the proof of concept route.

We were experiencing what looked like a problem this thread was discussing , but after a few days of trying to identify when our app hung on re-start we finally found another discussion on the interweb that pointed us to the problem and the solution.

In our case We were just unlucky to have started our Realm project using a version of Realm that would not start up due to an expired authentication token, which expires after 30 minutes.

Upgrading to the latest version of Realm corrected this behaviour and it now works exactly as expected.

Thanks

1 Like