Skip to content

Commit b7cc4af

Browse files
SyncClient: support adding filter variables in builder #280
1 parent c3e8630 commit b7cc4af

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Arrays;
2020
import java.util.Collections;
2121
import java.util.List;
22+
import java.util.Map;
23+
import java.util.TreeMap;
2224

2325
import javax.annotation.Nullable;
2426

@@ -57,6 +59,8 @@ public final class SyncBuilder {
5759
boolean uncommittedAcks;
5860

5961
RequestUpdatesMode requestUpdatesMode = RequestUpdatesMode.AUTO;
62+
// To be helpful when debugging, use a TreeMap so variables are eventually passed ordered by name to the native API
63+
final Map<String, String> filterVariables = new TreeMap<>();
6064

6165
public enum RequestUpdatesMode {
6266
/**
@@ -137,6 +141,22 @@ String serverUrl() {
137141
return url;
138142
}
139143

144+
/**
145+
* Adds or replaces a <a href="https://sync.objectbox.io/sync-server/sync-filters">Sync filter</a> variable value
146+
* for the given name.
147+
* <p>
148+
* Sync client filter variables can be used in server-side Sync filters to filter out objects that do not match the
149+
* filter.
150+
*
151+
* @see SyncClient#putFilterVariable
152+
*/
153+
public SyncBuilder filterVariable(String name, String value) {
154+
checkNotNull(name, "Filter variable name is null.");
155+
checkNotNull(value, "Filter variable value is null.");
156+
filterVariables.put(name, value);
157+
return this;
158+
}
159+
140160
/**
141161
* Configures a custom set of directory or file paths to search for trusted certificates in.
142162
* The first path that exists will be used.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public interface SyncClient extends Closeable {
128128
void setSyncTimeListener(@Nullable SyncTimeListener timeListener);
129129

130130
/**
131-
* Adds or replaces a Sync filter variable value for the given name.
131+
* Adds or replaces a <a href="https://sync.objectbox.io/sync-server/sync-filters">Sync filter</a> variable value
132+
* for the given name.
132133
* <p>
133134
* Eventually, existing values for the same name are replaced.
134135
* <p>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.objectbox.sync;
1818

19+
import java.util.Map;
1920
import java.util.concurrent.CountDownLatch;
2021
import java.util.concurrent.TimeUnit;
2122

@@ -71,6 +72,10 @@ public final class SyncClientImpl implements SyncClient {
7172
}
7273
this.handle = handle;
7374

75+
for (Map.Entry<String, String> entry : builder.filterVariables.entrySet()) {
76+
putFilterVariable(entry.getKey(), entry.getValue());
77+
}
78+
7479
// Only change setting if not default (automatic sync updates and push subscription enabled).
7580
if (builder.requestUpdatesMode != RequestUpdatesMode.AUTO) {
7681
boolean autoRequestUpdates = builder.requestUpdatesMode != RequestUpdatesMode.MANUAL;

0 commit comments

Comments
 (0)