File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
objectbox-java/src/main/java/io/objectbox/reactive Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change 1
1
package io .objectbox .reactive ;
2
2
3
+ import java .util .ArrayList ;
3
4
import java .util .List ;
4
5
6
+ /**
7
+ * Tracks any number of {@link DataSubscription} objects, which can be canceled with a single {@link #cancel()} call.
8
+ * This is typically used in live cycle components like Android's Activity:
9
+ * <ul>
10
+ * <li>Make DataSubscriptionList a field</li>
11
+ * <li>Call {@link #add(DataSubscription)} during onStart/onResume for each subscription</li>
12
+ * <li>Call {@link #cancel()} during onStop/onPause</li>
13
+ * </ul>
14
+ */
5
15
public class DataSubscriptionList implements DataSubscription {
16
+ private final List <DataSubscription > subscriptions = new ArrayList <>();
6
17
private boolean canceled ;
7
- List <DataSubscription > subscriptions ;
8
18
19
+ /** Add the given subscription to the list of tracked subscriptions. Clears any previous "canceled" state. */
9
20
public synchronized void add (DataSubscription subscription ) {
10
21
subscriptions .add (subscription );
22
+ canceled = false ;
11
23
}
12
24
25
+ /** Cancels all tracked subscriptions and removes all references to them. */
13
26
@ Override
14
27
public synchronized void cancel () {
15
28
canceled = true ;
@@ -19,11 +32,13 @@ public synchronized void cancel() {
19
32
subscriptions .clear ();
20
33
}
21
34
35
+ /** Returns true if {@link #cancel()} was called without any subsequent calls to {@link #add(DataSubscription)}. */
22
36
@ Override
23
37
public synchronized boolean isCanceled () {
24
38
return canceled ;
25
39
}
26
40
41
+ /** Returns number of active (added) subscriptions (resets to 0 after {@link #cancel()}). */
27
42
public synchronized int getActiveSubscriptionCount () {
28
43
return subscriptions .size ();
29
44
}
You can’t perform that action at this time.
0 commit comments