-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathFlexibleSyncExamples.cs
359 lines (323 loc) · 13.1 KB
/
FlexibleSyncExamples.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using Realms;
using Realms.Sync;
using MongoDB.Bson;
using System.Linq;
using NUnit.Framework;
using System.Threading.Tasks;
using Realms.Exceptions.Sync;
using Realms.Sync.Exceptions;
using static Realms.ThreadSafeReference;
namespace Examples
{
public class FlexibleSyncExamples
{
public async Task TestUseFlexibleSync()
{
var app = App.Create(Config.FSAppId);
var user = await app.LogInAsync(Credentials.Anonymous());
var config = new FlexibleSyncConfiguration(app.CurrentUser!);
var realm = Realm.GetInstance(config);
var subscriptions = realm.Subscriptions;
realm.Subscriptions.Update(() =>
{
// subscribe to all long running items, and give the subscription the name 'longRunningItems'
var longRunningItemsQuery = realm.All<MyTask>()
.Where(i => i.Status == "completed" && i.ProgressMinutes > 120);
realm.Subscriptions
.Add(longRunningItemsQuery,
new SubscriptionOptions() { Name = "longRunningItems" });
// subscribe to all of Ben's Item objects
realm.Subscriptions.Add(realm.All<MyTask>().Where(i => i.Owner == "Ben"));
// subscribe to all Teams, and give the subscription the name 'teamsSubscription' and throw an error if a new query is added to the team subscription
realm.Subscriptions.Add(realm.All<Team>(), new SubscriptionOptions() { Name = "teams", UpdateExisting = false });
});
// :snippet-start: wait-for-synchronization
try
{
await realm.Subscriptions.WaitForSynchronizationAsync();
}
catch (SubscriptionException ex)
{
// do something in response to the exception or log it
Console.WriteLine($@"The subscription set's state is Error and synchronization is paused: {ex.Message}");
}
// :snippet-end:
realm.Subscriptions.Update(() =>
{
var updatedLongRunningItemsQuery = realm
.All<MyTask>()
.Where(i => i.Status == "completed" && i.ProgressMinutes > 130);
realm.Subscriptions
.Add(updatedLongRunningItemsQuery,
new SubscriptionOptions() { Name = "longRunningItems" });
});
// :snippet-start: remove-subscription-by-query
// :replace-start: {
// "terms": {
// "MyTask": "Item"}
// }
realm.Subscriptions.Update(() =>
{
// remove a subscription by it's query
var query = realm.All<MyTask>().Where(i => i.Owner == "Ben");
realm.Subscriptions.Remove(query);
});
// :replace-end:
// :snippet-end:
// :snippet-start: remove-subscription-by-name
realm.Subscriptions.Update(() =>
{
// remove a named subscription
var subscriptionName = "longRunningItemsSubscription";
realm.Subscriptions.Remove(subscriptionName);
});
// :snippet-end:
// :snippet-start: remove-all-subscriptions-of-object-type
realm.Subscriptions.Update(() =>
{
// remove all subscriptions of the "Team" Class Name
realm.Subscriptions.RemoveAll("Team");
// Alernatively, remove all subscriptions of the "Team" object type
realm.Subscriptions.RemoveAll<Team>();
});
// :snippet-end:
// :snippet-start: remove-all-subscriptions
realm.Subscriptions.Update(() =>
{
// remove all subscriptions, including named subscriptions
realm.Subscriptions.RemoveAll(true);
});
// :snippet-end:
// :snippet-start: update-a-subscription
// :replace-start: {
// "terms": {
// "MyTask": "Item"}
// }
realm.Subscriptions.Update(() =>
{
var updatedLongRunningTasksQuery = realm.All<MyTask>()
.Where(t => t.Status == "completed" && t.ProgressMinutes > 130);
realm.Subscriptions.Add(updatedLongRunningTasksQuery,
new SubscriptionOptions() { Name = "longRunningTasks" });
});
// :replace-end:
// :snippet-end:
}
[Test]
public async Task TestOpenFSRealm()
{
var app = App.Create(Config.FSAppId);
var user = await app.LogInAsync(Credentials.Anonymous(false));
Realm realm;
// :snippet-start: open-fs-realm
// :replace-start: {
// "terms": {
// "MyTask" : "Item",
// "Config.FSAppId": "\"myRealmAppId\"",
// "MyTask": "Item",
// "Credentials.Anonymous(false)": "Credentials.Anonymous()"}
// }
var config = new FlexibleSyncConfiguration(user)
{
PopulateInitialSubscriptions = (realm) =>
{
var allItems = realm.All<MyTask>();
realm.Subscriptions.Add(allItems, new SubscriptionOptions { Name = "allItems" });
}
};
try
{
realm = await Realm.GetInstanceAsync(config);
// :remove-start:
var session = realm.SyncSession;
Assert.NotNull(session);
// :remove-end:
}
catch (Exception ex)
{
Console.WriteLine($@"Error creating or opening the
realm file. {ex.Message}");
}
// :replace-end:
// :snippet-end:
await user.LogOutAsync();
}
[Test]
public async Task TestOpenFSRealmOffline()
{
// :snippet-start: open-fs-realm-offline
// :replace-start: {
// "terms": {
// "Config.FSAppId": "\"myRealmAppId\"",
// "Credentials.Anonymous(false)": "Credentials.Anonymous()"
// }
// }
var app = App.Create(Config.FSAppId);
// :remove-start:
// Another app.CurrentUser was carrying over into this test
// causing issues with opening the FS realm. This resolves the
// test failure but there should probably be stronger cleanup
// between tests to negate the need for this.
if (app.CurrentUser != null)
{
await app.RemoveUserAsync(app.CurrentUser);
await app.LogInAsync(Credentials.Anonymous(false));
};
// :remove-end:
Realms.Sync.User user;
FlexibleSyncConfiguration config;
Realm realm;
if (app.CurrentUser == null)
{
// App must be online for user to authenticate
user = await app.LogInAsync(Credentials.Anonymous(false));
config = new FlexibleSyncConfiguration(user);
realm = Realm.GetInstance(config);
// Go on to add or update subscriptions and use the realm
// :remove-start:
var session = realm.SyncSession;
Assert.NotNull(session);
// :remove-end:
}
else
{
// This works whether online or offline
// It requires a user to have been previously authenticated
user = app.CurrentUser;
config = new FlexibleSyncConfiguration(user);
realm = Realm.GetInstance(config);
// Go on to add or update subscriptions and use the realm
}
// :replace-end:
// :snippet-end:
}
[Test]
public async Task TestCancelAsyncOperationsOnNonFatalErrors()
{
var app = App.Create(Config.FSAppId);
var user = await app.LogInAsync(Credentials.Anonymous());
// :snippet-start: appconfigsettings
// :replace-start: {
// "terms": {
// "Config.FSAppId": "\"myRealmAppId\""}
// }
AppConfiguration configuration = new AppConfiguration(Config.FSAppId)
{
SyncTimeoutOptions = new SyncTimeoutOptions()
{
ConnectTimeout = TimeSpan.FromMinutes(2),
ConnectionLingerTime = TimeSpan.FromSeconds(30),
PingKeepAlivePeriod = TimeSpan.FromMinutes(1),
PongKeepAliveTimeout = TimeSpan.FromMinutes(1),
FastReconnectLimit = TimeSpan.FromMinutes(1),
},
};
// :replace-end:
// :snippet-end:
// :snippet-start: cancelasync
var config = new FlexibleSyncConfiguration(app.CurrentUser!)
{
CancelAsyncOperationsOnNonFatalErrors = true,
};
// These operations will throw an exception
// on timeout or other transient sync session errors.
// :uncomment-start:
//var realm = await Realm.GetInstanceAsync(config);
//var session = realm.SyncSession;
//await session.WaitForUploadAsync();
//await session.WaitForDownloadAsync();
// :uncomment-end:
// :snippet-end:
}
public void WeeSnippetForDocs()
{
var realm = Realm.GetInstance();
// :snippet-start: example_sub
realm.Subscriptions.Update(() =>
{
var completedItemsQuery = realm
.All<MyTask>()
.Where(i => i.Status == "completed");
realm.Subscriptions
.Add(completedItemsQuery,
new SubscriptionOptions() { Name = "completedItems" });
});
// :snippet-end:
}
//[Test]
public async Task MoreFlexSyncExamples()
{
var app = App.Create(Config.FSAppId);
var user = await app.LogInAsync(Credentials.Anonymous());
var config = new FlexibleSyncConfiguration(app.CurrentUser!);
var realm = Realm.GetInstance(config);
// :snippet-start: subasync
var query = realm.All<Team>().Where(t => t.Name == "MyTeam");
await query.SubscribeAsync();
// you can also pass a SubscriptionOptions object:
var query2 = realm.All<Team>().Where(t => t.Name == "DevelopmentTeam");
await query2.SubscribeAsync(
new SubscriptionOptions() { Name = "devTeamSubscription" });
// :snippet-end:
realm.Dispose();
realm = Realm.GetInstance(config);
// :snippet-start: update-multiple-subscriptions
// :replace-start: {
// "terms": {
// "MyTask": "Item"}
// }
realm.Subscriptions.Update(() =>
{
// Subscribe to all long running items, and name
// the subscription "longRunningItems"
var longRunningTasksQuery = realm.All<MyTask>()
.Where(t => t.ProgressMinutes > 120);
realm.Subscriptions.Add(longRunningTasksQuery,
new SubscriptionOptions() { Name = "longRunningItems" });
// Subscribe to all of Ben's Items
realm.Subscriptions.Add(realm.All<MyTask>()
.Where(t => t.Owner == "Ben"));
// Subscribe to all Teams, name the subscription
// 'teamsSubscription', and throw an error if
// this subscription name already exists.
realm.Subscriptions.Add(realm.All<Team>(),
new SubscriptionOptions()
{ Name = "teams", UpdateExisting = false });
});
// :replace-end:
// :snippet-end:
Assert.AreEqual(5, realm.Subscriptions.Count);
}
}
partial class MyTask : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("name")]
public string Name { get; set; }
[MapTo("status")]
public string Status { get; set; }
[MapTo("owner")]
public string Owner { get; set; }
[MapTo("progressMinutes")]
public int ProgressMinutes { get; set; }
}
public enum ItemStatus
{
Open,
InProgress,
Complete
}
partial class Team : IRealmObject
{
[PrimaryKey]
[MapTo("_id")]
public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
[MapTo("name")]
public string Name { get; set; }
[MapTo("description")]
public string Description { get; set; }
}
}