I have 4 API calls in the same activity. 3 of them are independent of each other.I would like to call number 4 after first three finished and I am not sure the execution of first 3 in every time. I get data from database then it will call. It may 1 API call or 2 or 3 among first three.
I tried to call one after another sequentially but sometimes number 4 starts before first 3 finished. some of my efforts given below:
if(true){ // data 1 is available in database
firstRetrofitCall();
}else{
//show no data
}
if(true){ // data 2 is available in database
secondRetrofitCall();
}else{
//show no data
}
if(true){ // data 3 is available in database
thirdRetrofitCall();
}else{
//show no data
}
fourthRetrofitCall(); // I would like to execute this after first three finished
is it possible to manage using RxJava?
Use Rxjava2 adapter with Retrofit and then you can use Rxjava's zip operator to combine first three calls like this(assuming your calls return X,Y,Z values respectively and XYZwrapper is just container for these) and then flatMap operator to do the fourth call.
Single.zip(
firstRetrofitCall(),
secondRetrofitCall(),
thirdRetrofitCall(),
Function3<X, Y, Z, XYZwrapper> { x, y, z -> return#Function3 XYZwrapper(x, y, z) }
)
.subscribeOn(Schedulers.io())
.flatMap { XYZwrapper -> fourthRetrofitCall().subscribe() }//chaining
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy( onError = {}, onSuccess = {})
Declare a Boolean array of size 3 and initialize its indexes to false. Update the index to true in each 1st three API call's onResponse method. For example set index 0 to true for API call 1 and so on. And check in onResponse method that each of the array indexes are true if true then call the fourth API.
Add a boolean flag for each of those calls
boolean isFirstExecuted;
boolean isSecondExecuted;
boolean isThirdExecuted;
if(true){ // data 1 is available in database
firstRetrofitCall();
}else{
isFirstExecuted = true;
}
if(true){ // data 2 is available in database
secondRetrofitCall();
}else{
isSecondExecuted = true;
}
if(true){ // data 3 is available in database
thirdRetrofitCall();
}else{
isThirdExecuted = true;
}
checkAndExceuteFourth();
onFirstResponse(){
isFirstExecuted = true;
checkAndExceuteFourth();
}
onSecondResponse(){
isSecondExecuted = true;
checkAndExceuteFourth();
}
onThirdResponse(){
isThirdExecuted = true;
checkAndExceuteFourth();
}
Method for checking and executing fourth
public void checkAndExceuteFourth(){
if(isFirstExecuted && isFirstExecuted && isFirstExecuted ){
fourthRetrofitCall();
}
}
Related
I am creating a RxJava2 chain where in I want to enable and disable notification. the flow I am setting is as follows.
establish a connection.
set the notification to READ_STATUS UUID.
if the returned byte is zero then perform a write byte 01 to WRITE_STATUS UUID and after WRITE_STATUS, enable the notification of READ_STATUS UUID to verify it has byte value 1.
else if the returned byte is 1 then just enable other indicators (UUID1, UUID2,UUD3) and read the value.
I have a problem at step 2 and 3 where I am reading the value of READ_STATUS UUID by enabling the notification. in order to re-read the value, I probably need to disable the notification and then again enable it. And to disable to the notification I have to dispose that particular setupNotification .
Code is as follows
connectDisposable=
device.establishConnection(false)
.flatMap(rxBleConnection -> {
rxBleConnection.discoverServices();
mRxBleConnection = rxBleConnection;
return Observable.just(rxBleConnection);
})
.flatMap(rxBleConnection ->mRxBleConnection.setupNotification(READ_STATUS,NotificationSetupMode.QUICK_SETUP).flatMap(it->it))
.takeUntil(bytes -> {
if(getByteValue(bytes)==0)
return false;// dispose above to disable the notification
else
return true; // no need to disable the notification and continue writing
})
.flatMap(bytes -> {
return Observable.zip(
mRxBleConnection.writeCharacteristic(WRITE_STATUS, new byte[]{1}).toObservable(),
// setupNotification again to check whether read status has 1 or not
mRxBleConnection.setupNotification(READ_STATUS, NotificationSetupMode.QUICK_SETUP).flatMap(it->it),
Pair::new
);
})
.flatMap(bytes ->{
byte [] val= bytes.first;
if(getByteValue(val) == 1){
return Observable.zip(
mRxBleConnection.setupIndication(HISTORY, NotificationSetupMode.QUICK_SETUP).doOnNext(observable -> Log.e(TAG,"Here 1 ")).flatMap(it -> it),
mRxBleConnection.setupIndication(PARAMCHECK, NotificationSetupMode.QUICK_SETUP).doOnNext(observable -> Log.e(TAG,"Here 2 ")).flatMap(it -> it),
mRxBleConnection.setupIndication(FAULTINFO, NotificationSetupMode.QUICK_SETUP).doOnNext(observable -> Log.e(TAG,"Here 3 ")).flatMap(it -> it),
Data::Readings);
}
return Observable.empty();
}).subscribe(data -> {
});
The problem with this code is my takeUntil is firing at the last it does not dispose the previous setupNotificaion operation so that I can re read it later.
I tried solution mentioned over this thread but unfortunately I am not sharing the RxBleConnection
The problem with this code is my takeUntil is firing at the last it does not dispose the previous setupNotificaion operation so that I can re read it later.
The problem is that your condition is inverted. From .takeUntil() Javadoc:
* #return an Observable that first emits items emitted by the source Observable, checks the specified
* condition after each item, and then completes when the condition is satisfied.
You have used:
.takeUntil(bytes -> {
if(getByteValue(bytes)==0)
return false;// dispose above to disable the notification
else
return true; // no need to disable the notification and continue writing
})
where it should be satisfied (return true) when the upstream should get disposed:
.takeUntil(bytes -> {
if(getByteValue(bytes)==0)
return true;// dispose above to disable the notification
else
return false; // no need to disable the notification and continue writing
})
To unsubscribe or to dispose setupNotification or setupIndication one can use the following code. I am sure there could be different ways but so far I could find this
private Observable<Pair<byte[],byte[]>> getValueFromIndication(RxBleConnection rxBleConnection){
final PublishSubject<Boolean> unsubscribeOperation= PublishSubject.create();
return Observable.zip(
rxBleConnection.setupIndication(TSDictionary.FAULT_RETRY_COUNT_SEQUENCE,NotificationSetupMode.QUICK_SETUP).flatMap(it->it).takeUntil(unsubscribeOperation),
rxBleConnection.setupIndication(TSDictionary.FAULT_RETRY_INFORMATION,NotificationSetupMode.QUICK_SETUP).flatMap(it->it).takeUntil(unsubscribeOperation),
(bytes, bytes2) -> {
unsubscribeOperation.onNext(true);
return Pair.create(bytes,bytes2);
}
);
}
In above code, I am zipping two indication operations and once I get the value from it I am unsubscribing from the change chain using PublishSubject and takeUntil.
I have started migration from RecycledViewAdapter to PagedListAdapter and to accomplish this I'm using RxPagedListBuilder with ItemKeyedDataSource, BoundaryCallback and Placeholders enabled.
I am referring to Network Data with Database as Cache design which suggests that BoundaryCallback will be called when my DataSource (loads from the local database) runs out of data.
As it turns out, BoundaryCallback#onItemAtEndLoaded never gets called and I was quite puzzled.
My ItemKeyedDataSource returns my first loaded page (15 items) from the database + the totalCount of all available items fetch-able from the network (246) using callback(data, totalCount) on loadInitial()
When I scroll down my list, first 15 items appear as expected, but then I see a bunch of placeholders and no BoundaryCallback calls are made.
I have tried to analyze the code that should make a call to BoundaryCallback#onItemAtEndLoaded and the condition is as follows (PagedList):
private void tryDispatchBoundaryCallbacks(boolean post) {
...
final boolean dispatchEnd = mBoundaryCallbackEndDeferred
&& mHighestIndexAccessed >= size() - 1 - mConfig.prefetchDistance;
if (!dispatchBegin && !dispatchEnd) {
return;
}
if (dispatchEnd) {
mBoundaryCallbackEndDeferred = false;
}
if (post) {
mMainThreadExecutor.execute(new Runnable() {
#Override
public void run() {
dispatchBoundaryCallbacks(dispatchBegin, dispatchEnd);
}
});
} else {
dispatchBoundaryCallbacks(dispatchBegin, dispatchEnd);
}
}
You can see that the mHighestIndexAccessed has to be greater than size() - 1 - mConfig.PrefetchDistance; however, size() evaluates as follows:
mLeadingNullCount + mStorageCount + mTrailingNullCount
Simply put, it adds placeholders into the calculation (returns 246). So in case if 16th item is being accessed, this evaluates to 15 > 246 - 1 - 15 => 15 > 230. In other words, the BoundaryCallback will never invoke it's method unless I scroll all the way to the bottom (scrolling through all placeholders).
What am I missing?
I have code to listen to exactly three fields using Observables.combineLatest
Observables.combineLatest(text_name.asObservable(),text_username.asObservable(), text_email.asObservable()).subscribe({ t ->
if (t.first.toString() != currentName || t.second.toString() != currentUsername) {
startActionMode()
} else {
finishActionMode()
}
})
but when I add another parameter to the Observables.combineLatest it throughs error since only 3 inline-parameters can be passed..
Now I would wish to pass 4 parameters in the parameter list for Observables.combineLatest.. I know it should be done using an array or a list, passed in as parameter but It's hard for me to figure it out using Kotlin.
Help me out.. Thanks in Advance..
You need to add a combine function if you want to combine more than 3 observables. You can do something like this.
Observables.combineLatest(
first.asObservable(),
second.asObservable(),
third.asObservable(),
forth.asObservable()
)
// combine function
{ first, second, third, forth->
// verify data and return a boolean
return#subscribe first.toString() != currentName || second.toString() != currentUsername
}
.subscribe({ isValid->
if (isValid) {
startActionMode()
} else {
finishActionMode()
}
})
In the combine function you can verify your data and return a boolean.
Then in subscribe you can take an action based on that boolean
I am currently implementing a protocol for a Bluetooth device and i am using the RxAndroidBle Library (version 1.4.3).
I have to request data from the device by writing to characteristic and then listening to the response via a characteristic notification.
To combine the 2 operations (writing and listening) I am using the code from: https://stackoverflow.com/a/41140523/734385
connectionObservable
.flatMap( // when the connection is available...
rxBleConnection -> rxBleConnection.setupNotification(AP_SCAN_DATA), // ... setup the notification...
(rxBleConnection, apScanDataNotificationObservable) -> Observable.combineLatest( // ... when the notification is setup...
rxBleConnection.writeCharacteristic(AP_SCAN_DATA, writeValue), // ... write the characteristic...
apScanDataNotificationObservable.first(), // ... and observe for the first notification on the AP_SCAN_DATA
(writtenBytes, responseBytes) -> responseBytes // ... when both will appear return just the response bytes...
)
)
.flatMap(observable -> observable)
This approach works for me, the only problem is that the code gives me only the first 20 bytes (due to the apScanDataNotificationObservable.first()).
Unfortunately, I don't know the size of the package I am receiving. I can only extract the information from the header of the first 20 bytes. It seems like the RxJava buffer function all require to know the size beforehand.
Is there a way to make this work cleanly with the code above as part of the Rx chain?
In other words, can I control the number of emission based on the very first emission of an Rx chain?
Or do I have a completely wrong approach?
It is possible to achieve what you want.
The easiest way would be to exchange the Observable.combineLatest(...) to:
Observable.merge(
rxBleConnection.writeCharacteristic(AP_SCAN_DATA, writeValue).ignoreElements(), // send the request but ignore the returned value
apScanDataNotificationObservable.takeUntil(newResponseEndWatcher()) // take the response notifications until the response end watcher says so
);
Where newResponseEndWatcher() would need to contain the logic for determining if the received values are all that is expected. It could look like this:
private Func1<byte[], Boolean> newResponseEndWatcher() {
return new Func1<byte[], Boolean>() {
private static final int NOT_INITIALIZED = -1;
private int totalLength = NOT_INITIALIZED;
private int receivedLength = NOT_INITIALIZED;
#Override
public Boolean call(byte[] bytes) {
if (isNotInitialized(totalLength)) { // if it is the first received value
// parse totalLength from the header
}
// update receivedLength
return receivedLength >= totalLength;
}
private boolean isNotInitialized(int value) {
return value == NOT_INITIALIZED;
}
};
}
Just have in mind that Func1 which is the result newResponseEndWatcher() is stateful. If one would store into a variable the observable that is result of apScanDataNotificationObservable.takeUntil(newResponseEndWatcher()) the next subscriptions could end prematurely.
To mitigate this problem one may use Observable.using() function that would call newResponseEndWatcher() each time it is subscribed and then create a new apScanDataNotificationObservable.takeUntil(newResponseEndWatcher):
Observable.using(
() -> newResponseEndWatcher(), // create a new response end watcher on each subscription
responseEndWatcher -> apScanDataNotificationObservable.takeUntil(responseEndWatcher), // create the response observable that will complete properly
responseEndWatcher -> { /* ignored, responseEndWatcher will get GCed eventually */ }
);
I have posted all methods they are working separately , but I face issues with the first one, where I concatWith() two flowables
return userFavouriteStores()
.concatWith(userOtherStores())
.doOnNext(new Consumer<List<StoreModel>>() {
#Override
public void accept(#io.reactivex.annotations.NonNull List<StoreModel> storeModels) throws Exception {
Log.i("storeModels", "" + storeModels);
}
})
public Flowable<List<StoreModel>> userFavouriteStores() {
return userStores()
.map(UserStores::favoriteStores)
.flatMap(storeId -> storeDao.storesWithIds(storeId))
.map(stores -> { // TODO Konvert to Kotlin map {}
List<StoreModel> result = new ArrayList<>(stores.size());
for (se.ica.handla.repositories.local.Store store : stores) {
result.add(store.toStoreModel(StoreModel.Source.Favourite));
}
return result;
}); }
public Flowable<List<StoreModel>> userOtherStores() {
return userStores().map(UserStores::otherStores)
.flatMap(storeId -> storeDao.storesWithIds(storeId))
.map(stores -> {
List<StoreModel> result = new ArrayList<>(stores.size());
for (Store store : stores) {
result.add(store.toStoreModel(StoreModel.Source.Other));
}
return result;
});}
updated method :userStores() is used for favorite and other stores ,
private Flowable<UserStores> userStores() {
return apiIcaSeResource
.userStores()
.toFlowable(); }
#GET("user/stores")
Single<UserStores> userStores();
Following the comments follow up, and additional information, you don't have a problem specifically with the concat(), I'm assuming it is work, it's just not the tool for what you want to achieve here.
concat() will not concatenate two lists to a single list, but rathe will first emit all items by first Flowable and only then items emitted by second Flowable (hence you must have onComplete so concat will know when Flowable is end, what I asked in the begining).
in order to combine the lists together, I would suggest to zip both stores Obesrvables (favorites/ others), and then simply combine to list to have single output of combined list.
Besides that, as you pointed out, as both stores Observables comes from userStores(), you will invoke the network request twice, which definitely not necessary. you can solve it using publish(), that will share and multicast the network result to both Observables, resulting with single network request.
to sum it up, I would rather recommend to use Single here, not Flowable as you don't have backpressure consecrations. something like the following implementation:
Observable<List<StoreModel>> publish = userStores()
.toObservable()
.publish(userStores ->
Single.zip(
userFavouriteStores(userStores.singleOrError()),
userOtherStores(userStores.singleOrError()),
(favoriteStores, otherStores) -> {
favoriteStores.addAll(otherStores);
return favoriteStores;
}
)
.toObservable()
);