4
4
5
5
import javax .annotation .Nullable ;
6
6
7
+ import io .objectbox .annotation .apihint .Beta ;
7
8
import io .objectbox .annotation .apihint .Internal ;
8
9
9
10
/**
@@ -37,7 +38,7 @@ public class SubscriptionBuilder<T> {
37
38
private DataTransformer <T , Object > transformer ;
38
39
private Scheduler scheduler ;
39
40
private ErrorObserver errorObserver ;
40
- // private boolean sync;
41
+ // private boolean sync;
41
42
42
43
43
44
@ Internal
@@ -47,13 +48,13 @@ public SubscriptionBuilder(DataPublisher<T> publisher, @Nullable Object param, E
47
48
this .threadPool = threadPool ;
48
49
}
49
50
50
- // public Observable<T> runFirst(Runnable firstRunnable) {
51
- // if (firstRunnable != null) {
52
- // throw new IllegalStateException("Only one asyncRunnable allowed");
53
- // }
54
- // this.firstRunnable = firstRunnable;
55
- // return this;
56
- // }
51
+ // public Observable<T> runFirst(Runnable firstRunnable) {
52
+ // if (firstRunnable != null) {
53
+ // throw new IllegalStateException("Only one asyncRunnable allowed");
54
+ // }
55
+ // this.firstRunnable = firstRunnable;
56
+ // return this;
57
+ // }
57
58
58
59
/**
59
60
* Uses a weak reference for the observer.
@@ -75,10 +76,10 @@ public SubscriptionBuilder<T> onlyChanges() {
75
76
return this ;
76
77
}
77
78
78
- // public Observable<T> sync() {
79
- // sync = true;
80
- // return this;
81
- // }
79
+ // public Observable<T> sync() {
80
+ // sync = true;
81
+ // return this;
82
+ // }
82
83
83
84
/**
84
85
* Transforms the original data from the publisher to something that is more helpful to your application.
@@ -99,8 +100,9 @@ public <TO> SubscriptionBuilder<TO> transform(final DataTransformer<T, TO> trans
99
100
}
100
101
101
102
/**
102
- * The given {@link ErrorObserver} is notified when the {@link DataTransformer} ({@link #transform(DataTransformer)}) or
103
- * {@link DataObserver} ({@link #observer(DataObserver)}) threw an exception.
103
+ * The given {@link ErrorObserver} is notified when the {@link DataTransformer}
104
+ * ({@link #transform(DataTransformer)}) or {@link DataObserver} ({@link #observer(DataObserver)})
105
+ * threw an exception.
104
106
*/
105
107
public SubscriptionBuilder <T > onError (ErrorObserver errorObserver ) {
106
108
if (this .errorObserver != null ) {
@@ -126,6 +128,8 @@ public SubscriptionBuilder<T> on(Scheduler scheduler) {
126
128
127
129
/**
128
130
* The given observer is subscribed to the publisher. This method MUST be called to complete a subscription.
131
+ * <p>
132
+ * Note: you must keep the returned {@link DataSubscription} to cancel it.
129
133
*
130
134
* @return an subscription object used for canceling further notifications to the observer
131
135
*/
@@ -147,20 +151,31 @@ public DataSubscription observer(DataObserver<T> observer) {
147
151
observer = new ActionObserver (subscription );
148
152
}
149
153
150
- if (single ) {
151
- if (onlyChanges ) {
154
+ if (single ) {
155
+ if (onlyChanges ) {
152
156
throw new IllegalStateException ("Illegal combination of single() and onlyChanges()" );
153
157
}
154
158
publisher .publishSingle (observer , publisherParam );
155
159
} else {
156
160
publisher .subscribe (observer , publisherParam );
157
- if (!onlyChanges ) {
161
+ if (!onlyChanges ) {
158
162
publisher .publishSingle (observer , publisherParam );
159
163
}
160
164
}
161
165
return subscription ;
162
166
}
163
167
168
+ /**
169
+ * Convenience for calling {@link #observer(DataObserver)} with adding the resulting {@link DataSubscription} to the
170
+ * given {@link DataSubscriptionList}.
171
+ */
172
+ @ Beta
173
+ public DataSubscription observer (DataObserver <T > observer , DataSubscriptionList dataSubscriptionList ) {
174
+ DataSubscription dataSubscription = observer (observer );
175
+ dataSubscriptionList .add (dataSubscription );
176
+ return dataSubscription ;
177
+ }
178
+
164
179
class ActionObserver implements DataObserver <T >, DelegatingObserver <T > {
165
180
private final DataSubscriptionImpl subscription ;
166
181
private SchedulerRunOnError schedulerRunOnError ;
0 commit comments