Skip to content

Commit a7efa05

Browse files
Query: explain what subscribe does, add an example, add links
1 parent 4e2b553 commit a7efa05

File tree

1 file changed

+19
-14
lines changed
  • objectbox-java/src/main/java/io/objectbox/query

1 file changed

+19
-14
lines changed

objectbox-java/src/main/java/io/objectbox/query/Query.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2024 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2025 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
import io.objectbox.BoxStore;
3232
import io.objectbox.InternalAccess;
3333
import io.objectbox.Property;
34+
import io.objectbox.annotation.Entity;
3435
import io.objectbox.annotation.HnswIndex;
3536
import io.objectbox.exception.NonUniqueResultException;
3637
import io.objectbox.reactive.DataObserver;
@@ -918,22 +919,26 @@ public long remove() {
918919
}
919920

920921
/**
921-
* A {@link io.objectbox.reactive.DataObserver} can be subscribed to data changes using the returned builder.
922-
* The observer is supplied via {@link SubscriptionBuilder#observer(DataObserver)} and will be notified once
923-
* the query results have (potentially) changed.
922+
* Returns a {@link SubscriptionBuilder} to build a subscription to observe changes to the results of this query.
924923
* <p>
925-
* With subscribing, the observer will immediately get current query results.
926-
* The query is run for the subscribing observer.
924+
* Typical usage:
925+
* <pre>
926+
* DataSubscription subscription = query.subscribe()
927+
* .observer((List&lt;T&gt; data) -> {
928+
* // Do something with the returned results
929+
* });
930+
* // Once the observer should no longer be notified
931+
* subscription.cancel();
932+
* </pre>
933+
* Note that the observer will receive new results on any changes to the {@link Box} of the {@link Entity @Entity}
934+
* this queries, regardless of the conditions of this query. This is because the {@link QueryPublisher} used for the
935+
* subscription observes changes by using {@link BoxStore#subscribe(Class)} on the Box this queries.
927936
* <p>
928-
* Threading notes:
929-
* Query observers are notified from a thread pooled. Observers may be notified in parallel.
930-
* The notification order is the same as the subscription order, although this may not always be guaranteed in
931-
* the future.
937+
* To customize this or for advanced use cases, consider using {@link BoxStore#subscribe(Class)} directly.
932938
* <p>
933-
* Stale observers: you must hold on to the Query or {@link io.objectbox.reactive.DataSubscription} objects to keep
934-
* your {@link DataObserver}s active. If this Query is not referenced anymore
935-
* (along with its {@link io.objectbox.reactive.DataSubscription}s, which hold a reference to the Query internally),
936-
* it may be GCed and observers may become stale (won't receive anymore data).
939+
* See {@link SubscriptionBuilder#observer(DataObserver)} for additional details.
940+
*
941+
* @return A {@link SubscriptionBuilder} to build a subscription.
937942
*/
938943
public SubscriptionBuilder<List<T>> subscribe() {
939944
checkOpen();

0 commit comments

Comments
 (0)