Force-syncing realm and showing the progress meanwhile

Hey, thanks for the detailed post. I’ll try to address a few points here:

  1. To get progress notifications you can set the OnProgress property on your sync configuration. It’s a callback that gets invoked as data is being downloaded similarly to the session.GetProgressObservable code you have:
    var config = new SyncConfiguration(user, "my-partition")
    {
        OnProgress = progress =>
        {
            // Convert the reported bytes in a 0-100% range.
            var progressPercentage = 100.0 * progress.TransferredBytes / progress.TransferableBytes;
            this.ProgressIndicator.Progress = progressPercentage;
        }
    };
    
  2. RefreshAsync does not do any synchronization with the server, but rather updates your local database with changes that may have happened on background threads. This happens automatically when you’re on the main thread and the thread is idle, but you can use RefreshAsync to force update it. An example of this would be, if you download some data via a REST call, insert it on a background thread to avoid blocking the main thread with a large write, then want to lookup the newly inserted objects on the main thread. Calling await RefreshAsync will ensure that the newly inserted objects are available without introducing race conditions.
  3. If you want to ensure the Realm is synchronized, you can use Session.WaitForDownloadAsync. It will resolve once the download has completed. It can be used independently or in combination with GetProgressObservable. Generally speaking, WaitForDownloadAsync is more efficient than tracking progress, so if you just want to ensure the Realm has been synchronized without displaying progress bars, this is the recommended approach.
  4. The IObservable returned by GetProgressObservable with ProgressMode.ForCurrentlyOutstandingWork will complete once the upload/download has completed. So rather than checking for transferred >= transferrable, you can provide a OnComplete callback when subscribing.

I hope that clarifies the questions you’ve had. Finally, I’d like to point you to the warnings on this docs page as those are in effect both when using GetProgressObservable and when using SyncConfiguration.OnProgress.

1 Like