@@ -126,6 +126,7 @@ class SyncChange {
126
126
}
127
127
128
128
/// Sync client is used to connect to an ObjectBox sync server.
129
+ /// Use through [Sync] .
129
130
class SyncClient {
130
131
final Store _store;
131
132
@@ -137,25 +138,28 @@ class SyncClient {
137
138
? _cSync
138
139
: throw StateError ('SyncClient already closed' );
139
140
140
- /// Creates a sync client associated with the given store and options.
141
+ /// Creates a Sync client associated with the given store and options.
141
142
/// This does not initiate any connection attempts yet: call start() to do so.
142
- SyncClient (this ._store, String serverUrl, SyncCredentials creds) {
143
+ SyncClient ._(
144
+ this ._store, List <String > serverUrls, SyncCredentials credentials) {
145
+ if (serverUrls.isEmpty) {
146
+ throw ArgumentError .value (
147
+ serverUrls, "serverUrls" , "must contain at least one server URL" );
148
+ }
149
+
143
150
if (! Sync .isAvailable ()) {
144
151
throw UnsupportedError (
145
152
'Sync is not available in the loaded ObjectBox runtime library. '
146
153
'Please visit https://objectbox.io/sync/ for options.' );
147
154
}
148
155
149
- final cServerUrl = serverUrl.toNativeUtf8 ();
150
- try {
151
- _cSync = checkObxPtr (
152
- C .sync1 (InternalStoreAccess .ptr (_store), cServerUrl.cast ()),
153
- 'failed to create sync client' );
154
- } finally {
155
- malloc.free (cServerUrl);
156
- }
156
+ _cSync = withNativeStrings (
157
+ serverUrls,
158
+ (ptr, size) => checkObxPtr (
159
+ C .sync_urls (InternalStoreAccess .ptr (_store), ptr, size),
160
+ 'failed to create Sync client' ));
157
161
158
- setCredentials (creds );
162
+ setCredentials (credentials );
159
163
}
160
164
161
165
/// Closes and cleans up all resources used by this sync client.
@@ -557,18 +561,23 @@ class Sync {
557
561
/// Returns true if the loaded ObjectBox native library supports Sync.
558
562
static bool isAvailable () => _syncAvailable;
559
563
560
- /// Creates a sync client associated with the given store and configures it
564
+ /// Creates a Sync client associated with the given store and configures it
561
565
/// with the given options. This does not initiate any connection attempts
562
566
/// yet, call [SyncClient.start()] to do so.
563
567
///
564
568
/// Before [SyncClient.start()] , you can still configure some aspects of the
565
569
/// client, e.g. its [SyncRequestUpdatesMode] mode.
566
570
static SyncClient client (
567
- Store store, String serverUri, SyncCredentials creds) {
571
+ Store store, String serverUrl, SyncCredentials credentials) =>
572
+ clientMultiUrls (store, [serverUrl], credentials);
573
+
574
+ /// Like [client] , but accepts a list of URLs to work with multiple servers.
575
+ static SyncClient clientMultiUrls (
576
+ Store store, List <String > serverUrls, SyncCredentials credentials) {
568
577
if (syncClientsStorage.containsKey (store)) {
569
578
throw StateError ('Only one sync client can be active for a store' );
570
579
}
571
- final client = SyncClient (store, serverUri, creds );
580
+ final client = SyncClient ._ (store, serverUrls, credentials );
572
581
syncClientsStorage[store] = client;
573
582
InternalStoreAccess .addCloseListener (store, client, client.close);
574
583
return client;
0 commit comments