-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathClientResetExamples.cs
195 lines (183 loc) · 7.05 KB
/
ClientResetExamples.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
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using Realms;
using Realms.Sync;
using RealmUser = Realms.Sync.User;
using User = Examples.Models.User;
using Realms.Sync.Exceptions;
using Realms.Sync.Testing;
using Realms.Sync.ErrorHandling;
using static Realms.Sync.SyncConfigurationBase;
namespace Examples
{
public class ClientResetExamples
{
App app;
RealmUser user;
const string myRealmAppId = Config.FSAppId;
App fsApp = null!;
Realm fsRealm = null!;
RealmUser fsUser = null!;
public ClientResetExamples()
{
fsRealm = Realm.GetInstance();
}
[Test]
public async Task TestDiscardUnsyncedChangesHandler()
// :snippet-start: DiscardUnsyncedChangesHandler
{
// :remove-start:
app = App.Create(myRealmAppId);
user = await app.LogInAsync(Credentials.Anonymous(false));
// :remove-end:
var config = new FlexibleSyncConfiguration(user);
config.ClientResetHandler = new DiscardUnsyncedChangesHandler()
{
// The following callbacks are optional
OnBeforeReset = (beforeReset) =>
{
// Executed before the client reset begins
// Can be used to notify the user that a reset is going
// to happen
},
OnAfterReset = (beforeReset, afterReset) =>
{
// Executed after the client reset is complete
// Can be used to notify the user that the reset is done
},
ManualResetFallback = (err) =>
{
// Automatic reset failed; handle the reset manually here
}
};
try
{
var realm = await Realm.GetInstanceAsync(config);
}
catch (Exception ex)
{
Console.WriteLine($@"Error creating or opening the
realm file. {ex.Message}");
}
// :snippet-end:
await user.LogOutAsync();
}
public async Task TestManualClientReset()
// :snippet-start: ManualClientReset
// :replace-start: {
// "terms": {
// "fsApp": "app",
// "fsUser":"user",
// "fsConfig":"config",
// "fsrealm":"realm"
// }
// }
// :uncomment-start:
// private void SetupRealm()
// :uncomment-end:
{
//:remove-start:
fsApp = App.Create(myRealmAppId);
fsUser = fsApp.LogInAsync(
Credentials.EmailPassword(Config.Username, Config.Password)).Result;
//:remove-end:
var fsConfig = new FlexibleSyncConfiguration(fsUser);
fsConfig.ClientResetHandler =
new ManualRecoveryHandler(HandleClientResetError);
var fsrealm = await Realm.GetInstanceAsync(fsConfig);
}
private void HandleClientResetError(ClientResetException clientResetException)
{
Console.WriteLine($"Client Reset requested: {clientResetException.Message}");
// Prompt user to perform a client reset immediately. If they don't,
// they won't receive any data from the server until they restart the app
// and all changes they make will be discarded when the app restarts.
var didUserConfirmReset = ShowUserAConfirmationDialog();
if (didUserConfirmReset)
{
// Close the Realm before doing the reset. It must be
// deleted as part of the reset.
fsRealm.Dispose();
// perform the client reset
var didReset = clientResetException.InitiateClientReset();
if (didReset)
{
// Navigate the user back to the main page or reopen the
// the Realm and reinitialize the current page
}
else
{
// Reset failed - notify user that they'll need to
// update the app
}
}
}
// :replace-end:
// :snippet-end:
private bool ShowUserAConfirmationDialog()
{
return true;
}
public void TestRecoverOrDiscardUnsyncedChangesHandler()
{
// :snippet-start: RecoverOrDiscardUnsyncedChangesHandler
var conf = new FlexibleSyncConfiguration(user)
{
ClientResetHandler = new RecoverOrDiscardUnsyncedChangesHandler
{
// The following callbacks are optional
OnBeforeReset = (beforeReset) =>
{
// Executed before the client reset begins
// Can be used to notify the user that a reset is going
// to happen
},
OnAfterRecovery = (beforeReset, afterReset) =>
{
// Executed after the client reset is complete
// Can be used to notify the user that the reset is done
},
OnAfterDiscard = (beforeReset, afterReset) =>
{
// Executed if the automatic recovery has failed
// but the DiscardUnsyncedChanges fallback has completed
// successfully
},
ManualResetFallback = (err) =>
{
// Automatic reset failed; handle the reset manually here
}
}
};
// :snippet-end:
}
public void RecoverUnsyncedChangesHandler()
{
// :snippet-start: RecoverUnsyncedChangesHandler
var conf = new FlexibleSyncConfiguration(user)
{
ClientResetHandler = new RecoverUnsyncedChangesHandler
{
// The following callbacks are optional
OnBeforeReset = (beforeReset) =>
{
// Executed before the client reset begins
// Can be used to notify the user that a reset is going
// to happen
},
OnAfterReset = (beforeReset, afterReset) =>
{
// Executed after the client reset is complete
// Can be used to notify the user that the reset is done
},
ManualResetFallback = (err) =>
{
// Automatic reset failed; handle the reset manually here
}
}
};
// :snippet-end:
}
}
}