Skip to content

Commit 9b9b5fb

Browse files
greenrobot-teamgreenrobot
authored andcommitted
BoxStore: keep reference to not-closed SyncClient.
1 parent a3e68b1 commit 9b9b5fb

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import io.objectbox.reactive.DataObserver;
5454
import io.objectbox.reactive.DataPublisher;
5555
import io.objectbox.reactive.SubscriptionBuilder;
56+
import io.objectbox.sync.SyncClient;
5657

5758
/**
5859
* Represents an ObjectBox database and gives you {@link Box}es to get and put Objects of a specific type
@@ -244,6 +245,12 @@ public static boolean isSyncServerAvailable() {
244245

245246
private final TxCallback<?> failedReadTxAttemptCallback;
246247

248+
/**
249+
* Keeps a reference so the library user does not have to.
250+
*/
251+
@Nullable
252+
private SyncClient syncClient;
253+
247254
BoxStore(BoxStoreBuilder builder) {
248255
context = builder.context;
249256
relinker = builder.relinker;
@@ -1163,4 +1170,15 @@ public long getNativeStore() {
11631170
return handle;
11641171
}
11651172

1173+
/**
1174+
* Returns the {@link SyncClient} associated with this store. To create one see {@link io.objectbox.sync.Sync Sync}.
1175+
*/
1176+
@Nullable
1177+
public SyncClient getSyncClient() {
1178+
return syncClient;
1179+
}
1180+
1181+
public void setSyncClient(@Nullable SyncClient syncClient) {
1182+
this.syncClient = syncClient;
1183+
}
11661184
}

objectbox-java/src/main/java/io/objectbox/sync/SyncBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ public SyncClient build() {
141141
if (credentials == null) {
142142
throw new IllegalStateException("Credentials are required.");
143143
}
144+
if (boxStore.getSyncClient() != null) {
145+
throw new IllegalStateException("The given store is already associated with a Sync client, close it first.");
146+
}
144147
return new SyncClientImpl(this);
145148
}
146149

objectbox-java/src/main/java/io/objectbox/sync/SyncClientImpl.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.objectbox.sync;
22

3+
import io.objectbox.BoxStore;
34
import io.objectbox.InternalAccess;
45
import io.objectbox.annotation.apihint.Experimental;
56
import io.objectbox.annotation.apihint.Internal;
@@ -16,6 +17,8 @@
1617
@Internal
1718
public class SyncClientImpl implements SyncClient {
1819

20+
@Nullable
21+
private BoxStore boxStore;
1922
private final String serverUrl;
2023
private final InternalListener internalListener;
2124
@Nullable
@@ -28,6 +31,7 @@ public class SyncClientImpl implements SyncClient {
2831
private volatile boolean started;
2932

3033
SyncClientImpl(SyncBuilder builder) {
34+
this.boxStore = builder.boxStore;
3135
this.serverUrl = builder.url;
3236
this.connectivityMonitor = builder.platform.getConnectivityMonitor();
3337

@@ -58,6 +62,9 @@ public class SyncClientImpl implements SyncClient {
5862

5963
setLoginCredentials(builder.credentials);
6064

65+
// If created successfully, let store keep a reference so the caller does not have to.
66+
builder.boxStore.setSyncClient(this);
67+
6168
if (!builder.manualStart) {
6269
start();
6370
}
@@ -151,12 +158,22 @@ public synchronized void stop() {
151158

152159
@Override
153160
public void close() {
154-
if (connectivityMonitor != null) {
155-
connectivityMonitor.removeObserver();
156-
}
157-
158161
long handleToDelete;
159162
synchronized (this) {
163+
if (connectivityMonitor != null) {
164+
connectivityMonitor.removeObserver();
165+
}
166+
167+
// Remove instance reference from store, release store reference.
168+
BoxStore boxStore = this.boxStore;
169+
if (boxStore != null) {
170+
SyncClient syncClient = boxStore.getSyncClient();
171+
if (syncClient == this) {
172+
boxStore.setSyncClient(null);
173+
}
174+
this.boxStore = null;
175+
}
176+
160177
handleToDelete = this.handle;
161178
handle = 0;
162179
}

0 commit comments

Comments
 (0)