如何在初始同步时跟踪 MongoDB 领域中的进度?

How to track progress in MongoDB Realm on initial sync?

我计划使用离线优先方法开发一个 Windows 应用程序。我发现 MongoDB Realm with the .Net SDK 似乎符合我的要求。目前,我正在尝试了解如何在连接到服务器数据库时(尤其是第一次)跟踪 upload/download 进度。

我是这样连接的:

config = new SyncConfiguration("myPart", user); 
var realm = await Realm.GetInstanceAsync(config);

然后我在 Realm 文档中找到了这个片段来跟踪下载进度:

var token = session.GetProgressObservable(ProgressDirection.Download, ProgressMode.ReportIndefinitely)
                   .Subscribe(progress =>
                   {
                       if (progress.TransferredBytes < progress.TransferableBytes)
                       {
                           // Show progress indicator
                       }
                       else
                       {
                           // Hide the progress indicator
                       }
                   });

我的问题是,我需要一个会话句柄来订阅进度事件。但是我只有在打开领域(realm.GetSession())后才能获得会话。当我打开领域异步时,我不会在 Realm.GetInstanceAsync(config) 完成之前获得领域,这使得进度跟踪器无用。

同步过程中如何获取进度?谢谢

您可以指定 SyncConfiguration 的 OnProgress 属性:

config = new SyncConfiguration("myPart", user)
{
     OnProgress = progress =>
     {
         Console.WriteLine($"Progress: {progress.TransferredBytes}/{progress. TransferableBytes}");
     }
}

或者,您可以使用同步 realm.GetInstance 方法并立即注册 progress observable:

var realm = Realm.GetInstance(config);
realm.GetSession().GetProgressObservable(...)

OnProgess 属性 等同于 ProgressDirection.DownloadProgressMode.ForCurrentlyOutstandingWork.