Skip to content

Commit 16a0e85

Browse files
authored
Expose SyncConfiguration.OnProgress (#1807)
* Expose SyncConfiguration.OnProgress * Add docs and update PCL * update PR number * Fix the stylecop violation
1 parent 70e54eb commit 16a0e85

File tree

5 files changed

+73
-11
lines changed

5 files changed

+73
-11
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ x.y.z (2018-mm-dd)
22
------------------
33

44
### Enhancements
5-
* None.
5+
* Exposed an `OnProgress` property on `SyncConfigurationBase`. It allows you to specify a progress callback that will be invoked when using `Realm.GetInstanceAsync` to report the download progress. ([#1807](https://github.com/realm/realm-dotnet/pull/1807))
66

77
### Fixed
8-
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-dotnet/issues/????), since v?.?.?)
8+
<!-- * <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-dotnet/issues/????), since v?.?.?) -->
99
* Trying to call `Subscription.WaitForSynchronizationAsync` on a background thread (without a `SynchronizationContext`) would previously hang indefinitely. Now a meaningful exception will be thrown to indicate that this is not supported and this method should be called on a thread with a synchronization context. ([dotnet-private#130](https://github.com/realm/realm-dotnet-private/issues/130), since v3.0.0)
1010

1111
### Breaking changes

Platform.PCL/Realm.Sync.PCL/Configurations/SyncConfigurationBasePCL.cs

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public abstract class SyncConfigurationBase : RealmConfigurationBase
7272
/// </seealso>
7373
public string TrustedCAPath { get; set; }
7474

75+
/// <summary>
76+
/// Gets or sets a callback that is invoked when download progress is made when using <see cref="Realm.GetInstanceAsync"/>.
77+
/// This will only be invoked for the initial download of the Realm and will not be invoked as futher download
78+
/// progress is made during the lifetime of the Realm. It is ignored when using
79+
/// <see cref="Realm.GetInstance(RealmConfigurationBase)"/>.
80+
/// </summary>
81+
public Action<SyncProgress> OnProgress { get; set; }
82+
7583
internal SyncConfigurationBase(Uri serverUri, User user = null, string optionalPath = null)
7684
{
7785
RealmPCLHelpers.ThrowProxyShouldNeverBeUsed();

Realm/Realm.Sync/Configurations/SyncConfigurationBase.cs

+17-8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public abstract class SyncConfigurationBase : RealmConfigurationBase
7979
/// </seealso>
8080
public string TrustedCAPath { get; set; }
8181

82+
/// <summary>
83+
/// Gets or sets a callback that is invoked when download progress is made when using <see cref="Realm.GetInstanceAsync"/>.
84+
/// This will only be invoked for the initial download of the Realm and will not be invoked as futher download
85+
/// progress is made during the lifetime of the Realm. It is ignored when using
86+
/// <see cref="Realm.GetInstance(RealmConfigurationBase)"/>.
87+
/// </summary>
88+
public Action<SyncProgress> OnProgress { get; set; }
89+
8290
internal SyncConfigurationBase(Uri serverUri, User user = null, string optionalPath = null)
8391
{
8492
Argument.Ensure(user != null || User.AllLoggedIn.Length == 1,
@@ -104,14 +112,8 @@ internal SyncConfigurationBase(Uri serverUri, User user = null, string optionalP
104112
/// </summary>
105113
public static LogLevel LogLevel
106114
{
107-
get
108-
{
109-
return SharedRealmHandleExtensions.GetLogLevel();
110-
}
111-
set
112-
{
113-
SharedRealmHandleExtensions.SetLogLevel(value);
114-
}
115+
get => SharedRealmHandleExtensions.GetLogLevel();
116+
set => SharedRealmHandleExtensions.SetLogLevel(value);
115117
}
116118

117119
internal override Realm CreateRealm(RealmSchema schema)
@@ -135,12 +137,19 @@ internal override Realm CreateRealm(RealmSchema schema)
135137
internal override async Task<Realm> CreateRealmAsync(RealmSchema schema)
136138
{
137139
var session = new Session(SharedRealmHandleExtensions.GetSession(DatabasePath, ToNative(), EncryptionKey));
140+
IDisposable subscription = null;
138141
try
139142
{
143+
if (OnProgress != null)
144+
{
145+
var observer = new Observer<SyncProgress>(OnProgress);
146+
subscription = session.GetProgressObservable(ProgressDirection.Download, ProgressMode.ForCurrentlyOutstandingWork).Subscribe(observer);
147+
}
140148
await session.WaitForDownloadAsync();
141149
}
142150
finally
143151
{
152+
subscription?.Dispose();
144153
session.CloseHandle();
145154
}
146155

Tests/Tests.Sync.Shared/SessionTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ public void Session_Error_WhenInvalidAccessToken()
206206
[TestCase(ProgressMode.ReportIndefinitely)]
207207
public void Session_ProgressObservable_IntegrationTests(ProgressMode mode)
208208
{
209-
Console.WriteLine("Aaaaa");
210209
SyncTestHelpers.RequiresRos();
211210

212211
const int ObjectSize = 1000000;

Tests/Tests.Sync.Shared/SynchronizedInstanceTests.cs

+46
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,52 @@ public void GetInstanceAsync_CreatesNonExistentRealm()
181181
});
182182
}
183183

184+
[Test]
185+
public void GetInstanceAsync_ReportsProgress()
186+
{
187+
SyncTestHelpers.RequiresRos();
188+
189+
AsyncContext.Run(async () =>
190+
{
191+
var realmPath = Guid.NewGuid().ToString();
192+
var user = await SyncTestHelpers.GetUserAsync();
193+
var config = new FullSyncConfiguration(new Uri($"/~/{realmPath}", UriKind.Relative), user, Guid.NewGuid().ToString());
194+
const int ObjectSize = 1000000;
195+
const int ObjectsToRecord = 20;
196+
using (var realm = GetRealm(config))
197+
{
198+
for (var i = 0; i < ObjectsToRecord; i++)
199+
{
200+
realm.Write(() =>
201+
{
202+
realm.Add(new HugeSyncObject(ObjectSize));
203+
});
204+
}
205+
206+
await WaitForSyncAsync(realm);
207+
}
208+
209+
var callbacksInvoked = 0;
210+
211+
var lastProgress = default(SyncProgress);
212+
config = new FullSyncConfiguration(new Uri($"/~/{realmPath}", UriKind.Relative), user, Guid.NewGuid().ToString())
213+
{
214+
OnProgress = (progress) =>
215+
{
216+
callbacksInvoked++;
217+
lastProgress = progress;
218+
}
219+
};
220+
221+
using (var realm = await GetRealmAsync(config))
222+
{
223+
Assert.That(realm.All<HugeSyncObject>().Count(), Is.EqualTo(ObjectsToRecord));
224+
Assert.That(callbacksInvoked, Is.GreaterThan(0));
225+
Assert.That(lastProgress.TransferableBytes, Is.EqualTo(lastProgress.TransferredBytes));
226+
}
227+
});
228+
}
229+
184230
[TestCase(true, true)]
185231
[TestCase(true, false)]
186232
[TestCase(false, true)]

0 commit comments

Comments
 (0)