I have in my project Retrofit library and RxAndroid.
For example I've called some method from my api
public void loadSomething() {
getApi().getSomething()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Something>() {
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable e) {
}
#Override
public void onNext(Something s) {
}
});
}
How should I track progress of this observable for example to prevent user to start it second time?
Should add my custom flags something like before start set inProgress = true; and in all callback methods set inProgress = false;?
Can I do it with RxAndroid functionality? Or is it guarantee that one call to api will not be started few times in parallel?
You can use the Do operator.
You can set the inPogress = true in doOnSubscribe() and inProgress = false in doOnComplete().
Also calling subscribe returns a Subscription. You can use it to check if the call has finished using isUnsubscribed().
Related
I want to implement something like gate mechanism.
I need one PublishSubject and a couple of subscribers. When PublishSubject send data via onNext only one subscriber will receive it.
For example:
I have 3 equals fragments inside tabs. They have subscription to global published called onLoginPublisher.
When onResume or onPause called gate becomes open or closed.
When onLogin called and no gates are opened because of no one of these fragments on screen, onNext will wait for fragment's onResume
Look at the pic:
You can use filter with the gate's state. For example, you can wrap all the logic into a class:
public final class GatedSubject<T> {
final PublishSubject<T> subject = PublishSubject.create();
final AtomicReferenceArray<Boolean> gates;
public GatedSubject(int numGates) {
gates = new AtomicReferenceArray<>(numGates);
}
public boolean getGateStatus(int gateIndex) {
return gates.get(gateIndex) != null;
}
public void setGateStatus(int gateIndex, boolean status) {
gates.set(gateIndex, status ? Boolean.TRUE : null);
}
public void Observable<T> getGate(int gateIndex) {
return subject.filter(v -> getGateStatus(gateIndex));
}
public void onNext(T item) {
subject.onNext(item);
}
public void onError(Throwable error) {
subject.onError(error);
}
public void onComplete() {
subject.onComplete();
}
}
I have a DisposableSubscriber to a Flowable. The Flowable runs for some timeUntilTimeout, but in some situations I need to kill it earlier. Right now I call .dispose() on the DisposableSubscriber but the Flowable continues to emit events. Eventually the Flowable times out and .doOnCancel() is called.
I have the following code:
private Disposable mDisposableSubscription = null;
public void start() {
mDisposableSubscription = getFlowable()
.timeout(timeUntilTimeout, TimeUnit.MILLISECONDS)
.subscribeWith(new DisposableSubscriber<T>() {
#Override
public void onComplete() {
}
#Override
public void onError(Throwable throwable) {
dispose();
}
#Override
public void onNext(T t) {
// Do something
}
});
}
public void stop() {
// Log "dispose"
mDisposableSubscription.dispose();
}
private Flowable<T> getFlowable() {
return Flowable.create(new FlowableOnSubscribe<T>() {
public void subscribe(FlowableEmitter<T> emitter) {
startSomeAsyncProcess();
}
}).doOnCancel(new Action() {
public void run() {
// Log "do on cancel"
stopSomeAsyncProcess();
}
});
}
Calling stop() to dispose of the DisposableSubscriber before the Flowable times out means events emitted by the Flowable are no longer handled, but the Flowable continues emitting events and the async process continues running. I was under the impression that calling .dispose() downstream of the Flowable kills the Flowable by calling .doOnCancel(), but this does not appear to be the case. What am I missing?
The flowable is getting disposed, but you are not checking it on your Flowable.create function, so what happens is that the startSomeAsyncProcess() ignores it and keeps going.
To solve the issue, you should check the emitter.isDisposed() flag to know if you should stop emitting.
Example:
Flowable<T> getFlowable() {
return Flowable.create(new FlowableOnSubscribe<T>() {
public void subscribe(FlowableEmitter<T> emitter) {
while(!emitter.isDisposed()) {
emitter.onNext(...);
}
}
});
}
If that startSomeAsyncProcess() function doesn't allow you to check the flag, surely there is some way to cancel it. Then you can attach a cancellable:
Flowable<T> getFlowable() {
return Flowable.create(new FlowableOnSubscribe<T>() {
public void subscribe(FlowableEmitter<T> emitter) {
startSomeAsyncProcess();
emitter.setCancellable(() -> stopSomeAsyncProcess());
// I don't remember if it's setCancellable() or setDisposable()
}
});
}
Update: the methods setCancellable(...) and setDisposable(...) should behave equally, they just take different arguments.
I need a shorter & cleaner solution for Example 1. So multiple async calls need to be finished before a certain Activity/Fragment can start. Example 1 is very messy and ugly with member bools, but works.
I was considering using the Google Tasks API. But for that I need to add a google-services.json and connect to either "Google Sign-in", "Analytics" or "Cloud messaging", which I don't need I think. There must be a better way or is this the correct way to go?
Example 1:
boolean mIsFirstDone = false;
boolean mIsSecondDone = false;
boolean mAlreadyDone = false;
private void prepareSomeData() {
dataManager.requestSomeContent(new ApiCallback() {
#Override
public void onSuccess(final Object object) {
mIsFirstDone = true;
if(mIsFirstDone && mIsSecondDone && !mAlreadyDone) {
mAlreadyDone = true;
doSomething();
}
}
});
}
private void prepareSomeSettings() {
dataManager.requestSomeSettings(new ApiCallback() {
#Override
public void onSuccess(final Object object) {
mIsSecondDone = true;
if(mIsFirstDone && mIsSecondDone && !mAlreadyDone) {
mAlreadyDone = true;
doSomething();
}
}
});
}
With Tasks API:
Tasks.whenAll(SomeDataTask, SomeSettingsTask).addOnSuccessListener(executor, new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void v) {
doSomething();
}
}).addOnFailureListener(executor, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
RxJava, as pointed out, is probably a better solution to this. The reason why is because you can chain multiple api requests, database requests into a concrete block of code that looks elegant and clean. As an example, see below of what I'm trying to say:
Subscription subscription = apiService.getUser(someId)
.flatMap(user -> apiService.getFavourites(user.getFavouritesTag())
.subscribe(favourites -> view.updateFavouritesList(favourites),
throwable -> Log.e(TAG, throwable.printStackTrace());
Have you considered learning about RxJava and reformatting all your projects to RxJava along with retrofit for API?
start with something like this:
https://medium.com/yammer-engineering/chaining-multiple-sources-with-rxjava-20eb6850e5d9
https://adityaladwa.wordpress.com/2016/05/11/dagger-2-and-mvp-architecture/
I am learning RxJava and am testing a scenario where I read data from a DB and then post it to a Queue. I just made a sample mock of the whole process but I don't seem to find the Observable working as I wanted it to ie. asynchronously.
This is my code:
package rxJava;
import java.util.ArrayList;
import java.util.List;
import rx.Observable;
import rx.Observer;
import rx.functions.Action1;
public class TestClass {
public static void main(String[] args) {
TestClass test = new TestClass();
System.out.println("---START---");
test.getFromDB().subscribe(new Observer<String>() {
#Override
public void onCompleted() {
System.out.println("Publish complete.");
}
#Override
public void onError(Throwable t) {
System.out.println(t.getMessage());
}
#Override
public void onNext(String s) {
test.publishToQueue(s).subscribe(new Observer<Boolean>() {
#Override
public void onNext(Boolean b) {
if (b) {
System.out.println("Successfully published.");
}
}
#Override
public void onCompleted() {
}
#Override
public void onError(Throwable arg0) {
}
});
};
});
System.out.println("---END---");
}
public Observable<String> getFromDB() {
List<String> list = new ArrayList<String>();
for (int i = 0; i < 30; i++) {
list.add(Integer.toString(i));
}
return Observable.from(list).doOnNext(new Action1<String>() {
#Override
public void call(String temp) {
if (temp.contains("2")) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
public Observable<Boolean> publishToQueue(String s) {
return Observable.defer(() -> {
try {
if (s.contains("7")) {
Thread.sleep(700);
}
System.out.println("Published:: " + s);
} catch (InterruptedException e) {
e.printStackTrace();
}
return Observable.just(true);
});
}
}
Suppose I get a list from the DB asynchronously and want to post it to the queue,. I have used an Observable returned from getFromDB and have subscribed to it which mimics the data I get from DB. Every time I get the data from DB , I want to push it to a queue using publishToQueue which also returns an Observable. I wanted to make the queue call also asynchronous. Now on positive acknowledgement from the queue such as the Boolean which I am returning (Observable<Boolean>), I want to print something.
So basically I just want both the processes to be asynchronous. For every data from DB, I push it to the Queue asynchronously.
I have added Thread.sleep() in both the methods, db call and queue so as to mimic a delay and to test the asynchronous operations. I think this is what causing the problem. But I also tried Obseravable.delay() but that doesn't even produce any output.
Please help me understand how this works and how I can make it work as I want it to.
You have to specified subscribeOn value.
Observable.just("one", "two", "three", "four", "five")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* an Observer */);
http://reactivex.io/documentation/operators/subscribeon.html
By default, RxJava is synchronous. It means that everything will be perform in the same thread (and the current thread), by default. You can perform tasks in another thread thanks to observeOn / subscribeOn methods, or using some operators that perform tasks in another job (because it use another scheduler, like delay, interval, ...)
In your example, you have to explitly set in which scheduler the subscription will pe performed. (here, in which thread Observable.from will emit your list)
test.getFromDb()
.subscribeOn(Schedulers.io())
.subscribe();
Then you can use the flatMap operator and calling your publishToQueue method. This method will be executed in the previous scheduler, but you can force it to use another scheduler, thanks to observeOn method. Everything after the observeOn method will be executed in another thread.
test.fromDb()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.flatMap(l -> test.publishToqueue(l))
.subscribe();
I'm using a complex closed api. I want to create a super simple way to use it.
Basicaly, it has the following behaviour:
boolean everythingReady = false;
API.start(new Callback() {
public void onReady()
{
API.invite(new Callback2() {
public void onReady()
{
everythingReady = true;
}
});
}
});
while (!everythingReady); // Wait
API.send("hello);
API.send("What's up");
This is a chat API and the above code is "pseudo" java.
What I want to do now is:
API.start();
API.invite();
API.send("Hello);
API.send("What's up");
these methods would wait until each above has been correctly loaded (onReady called) to run. (eg: if (!apiStarted) addToQueue else do invite)
Is there a way to do that in java (as I can't edit at all the API sources).
Thanks
How about:
API.start(new Callback() {
public void onReady()
{
API.invite(new Callback() {
public void onReady()
{
API.send("hello");
API.send("What's up");
}
});
}
});
This way the send methods will be called only after invite is ready.