How can I listen to sync state for Flutter SDK?

I can’t seem to find any examples on how I would be able to listen to and get notifications on sync state (e.g. syncing, synced, disconnected). The only example in the document shows that I can listen to the sync progress on Realm.open(onProgressCallback) by passing a ProgressCallback.

Am I correct in assuming that ProgressCallback will continue to monitor the sync progress for the entire life of the opened realm? Or is it a once off check on first opened? When the realm has been synced on that initial open, will the callback be continued to get notifications around the progress? If this the case, I could potentially check that 0 transferabble data means it is synced. Meaning that the transferred bytes will jump up and down between 0 and some numbers throughout the life of the opened realm as it continues to sync updates in the background.

I would ideally want to check whether the realm is in a synced state to the remote server before I do some work. I could test the internet connection myself but it won’t guarantee that the realm has been synced.

Any help would be appreciated, thanks!

Hi @lHengl, Thanks for your interest to Ream Sync.
onProgressCallback occurs only while Realm.open is working. Once the Realm.open completes this event handler is dettached. You can be sure that the realm is fully synced after Realm.open completes.
If you want to be sure at any time that the realm is fully synced you can wait waitForUpload() and waitForDownload() to complete before to do your work.

 await realm.syncSession.waitForUpload();
 await realm.syncSession.waitForDownload();

If you don’t have internet connection these methods will continue to wait until the connection becomes available and then they will complete.

If you want to be sure that the syncing is not in progress while you are doing your work you can use “pause” to stop syncing:

realm.syncSession.pause();

And then after you finish the work call “resume” to allow syncing:

realm.syncSession.resume();

I hope this answer is useful for your scenario.
If not, feel free to write!

@lHengl
You can also take a look at the other options provided by SysncSession API:

  • state returning the current state of the session.
  • connectionState returning the current state of the connection.
  • connectionStateChanges returns a Stream that emits connection state updates.
  • pause() pauses synchronization.
  • resume() resumes synchronization.
  • waitForUpload/waitForDownload returns a Future that completes when the session uploaded/downloaded all changes.
  • getProgressStream returns a Stream that emits progress updates.
    For more information Session class - realm library - Dart API
    Maybe some of them can help for your task.

Perfect thanks, didn’t realise syncSession was a thing, I don’t remember seeing anything about it in the documentation.

This should do it, I will write some code and see how I go, thanks alot.