Single.fromCallable() - Chaining two calls, one after another - java

I'm using Single.fromCallable() to make a network call and I want to use the data from the response to make a second call.
How can I chain these two calls instead of nesting them?
private void queryForUser() {
Single.fromCallable(() -> remoteRepository.queryForUser()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(response-> {
String username = response.getUsername();
//Perform second call
performSecondQuery(username);
}, err -> {
Log.e(TAG, "Failed to get user", err);
});
}
private void performSecondQuery(String username){
Single.fromCallable(() -> remoteRepository.performSecondQuery(username)))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(secondQueryResult -> {
Log.d(TAG, "performSecondQuery: " + secondQueryResult);
}, err -> {
Log.e(TAG, "Failed to perform second query", err);
});
}

My Java lambda syntax is rusty, but using flatMap() should give you something like this:
private Single<Whatever> performSecondQuery(String username){
return Single.fromCallable(() -> remoteRepository.performSecondQuery(username)))
}
private void queryForUser() {
Single.fromCallable(() -> remoteRepository.queryForUser()))
.flatMap { response -> performSecondQuery(response.getUsername()) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
// TODO something with the result
}, err -> {
Log.e(TAG, "Failed to get user", err);
});
}
(note: I don't know what the second query is doing, so I don't know what Whatever is — it would be the return type of remoteRepository.performSecondQuery())

Related

How to return value from Observable to Rxjava 2

I ran into a problem that onNext cannot contain return, but I need to return the string.
The request is made using Retrofit with a factory inside the interface (ApiService).
fun getNameAnimal(name : String) : String {
var nameAnimal = " "
val api = ApiService.create()
api.getAnimal("Cat")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ animal ->
// It works
Log.i(LOG, animal.name)
// It NOT works (empty value)
nameAnimal = animal.name
},
{ error ->
Log.e(LOG, error.printStackTrace())
}
)
return nameAnimal
}
In the logs, the answer comes in the format I need.
The method is in a class that is not an activity or fragment.
How can I implement my plan?
fun getNameAnimal(name : String) : Single<String> {
val api = ApiService.create()
return api.getAnimal("Cat")
.map { animal -> animal.name }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
2. In activity or fragment
apiWorkingClassInstance.getNameAnimal()
.subscribe(
{ animalName ->
Log.i(LOG, animalName)
//todo
},
{ error ->
Log.e(LOG, error.printStackTrace())
}
)
Thanks for the tip Alex_Skvortsov
An easy and clean way to achieve this would be to use an interface.
Create your interface
interface AnimalCallbacks {
fun onAnimalNameReturned(name: String)
}
Have your class implement the interface
class MyAnimal: AnimalCallbacks {
override fun onAnimalNameReturned(name: String) {
//handle logic here
}
}
From here you can pass your interface in the method call and send the result back using the method you defined in the interface.
fun getNameAnimal(name : String, callbacks: AnimalCallbacks) {
var nameAnimal = " "
val api = ApiService.create()
api.getAnimal("Cat")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ animal ->
// It works
Log.i(LOG, animal.name)
// It NOT works (empty value)
nameAnimal = animal.name
callbacks.onAnimalNameReturned(animal.name)
},
{ error ->
Log.e(LOG, error.printStackTrace())
}
)
}

RxJava. Change chain in compose

I have the code:
return channelRepository.getRssFeedContent(channel.getSourceLink())
.toObservable()
.map(this::parseItem)
.flatMapIterable(xmlItemRawObjects -> xmlItemRawObjects)
.compose(/* question */)
.subscribeOn(Schedulers.from(threadExecutor))
.observeOn(postExecutionThread.getScheduler());
Using xmlItemRawObjects I need to make a query to the database, check if the record exists. If no record exists, return the same xmlItemRawObjects from compose() to continue working with it.
If the record exists in the database, then make sure that compose() skips the element for the stream.
I tried to create a function:
.compose(new ObservableTransformer<XmlItemRawObject, XmlItemRawObject>() {
#Override
public ObservableSource<XmlItemRawObject> apply(Observable<XmlItemRawObject> upstream) {
Boolean isExists = false;
Observable<Item> test = Observable.create(emitter -> {
upstream
.flatMap(xmlItemRawObject -> channelRepository.getItemByUniqueId(xmlItemRawObject.getGuid())
.subscribe(item -> isExists = true));
});
}
})
but it works just fine. Thank you for your advice.
P.S. No examples from compose() at all.
I've tried to solve the problem through flatmap:
.flatMap(new Function<XmlItemRawObject, ObservableSource<XmlItemRawObject>>() {
#Override
public ObservableSource<XmlItemRawObject> apply(XmlItemRawObject xmlItemRawObject) throws Exception {
Observable<XmlItemRawObject> test = Observable.create(emitter -> {
channelRepository.getItemByUniqueId(xmlItemRawObject.getGuid())
.subscribe(
item -> {
emitter.onComplete();
}, throwable -> {}, () -> Observable.just(xmlItemRawObject));
});
return test.defaultIfEmpty(xmlItemRawObject);
}})
Can't check an empty "subquery" result, make an if-else construct to pass on the xmlItemRawObject along the chain

Can't toast on a thread that has not called, RxJava2

I have this code:
compositeDisposable.add(RetrofitClient
.getApi()
.getData()
.flatMap(response -> {
Data data;
if (response.isSuccessful()) {
data = response.body();
//insert data to database
Database.getInstance(context)
.getDao()
.insert(data);
} else {
ResponseBody responseBody = response.errorBody();
if (responseBody != null) {
data = new Gson().fromJson(responseBody.charStream(), Data.class);
}
}
return Observable.just(data);
})
.onErrorResumeNext(throwable -> {
//get data from database
Data data = Database.getInstance(context).getDao().getData();
return Observable.just(data);
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retryWhen(throwableObservable ->
throwableObservable.take(1).delay(1, TimeUnit.SECONDS))
.doOnTerminate(view::hideScreenLoader)
.subscribe(this::showData, throwable -> {
Toast.makeText(context,
throwable.getMessage(), Toast.LENGTH_LONG).show();
}));
I can't understand why sometimes users got this error: "Caused by java.lang.RuntimeException Can't toast on a thread that has not called Looper.prepare()".
Toast must called at main thread, thanks.
I found the decision in this topic: how to handle RxAndroid errors in the main thread;
Need to swap "retryWhen" and "observeOn", because retryWhen has "delay function", that switch to computation thread :)

Return Observable in callback rxjava

I am messing around some with the google awareness api and now my understanding of RxJava is limiting me.
What I want to achieve in the end:
I want to get a Weather and a Location from the Api, and merge them into one object that I can pass on to my view for update.
However, I'm not sure how I achieve the returning of an Observable from the api callback here since it has void return type, and how to achieve merging of the weather and location object from api.getWeather and api.getLocation
public void requestUserCurrentInfo() {
Subscription userInfo = getWeatherLocation().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(userinfo ->
Log.d(TAG,userinfo.something()));
}
public Observable<UserInfo> getWeatherLocation () {
try {
Awareness.SnapshotApi.getWeather(client)
.setResultCallback(weather -> {
if (!weather.getStatus().isSuccess()) {
Log.d(TAG, "Could not get weather");
return;
}
//How do I do here?
return weather.getWeather();
});
Awareness.SnapshotApi.getLocation(mGoogleApiClient)
.setResultCallback(retrievedLocation -> {
if(!retrievedLocation.getStatus().isSuccess()) return;
Log.d("FRAG", retrievedLocation.getLocation().getLatitude() + "");
});
} catch (SecurityException exception) {
throw new SecurityException("No permission " + exception);
}
}
For my other things in my Project, I get some stuff through a REST api following the repository pattern, then I can get it like this because every step returns a Observable< SmhiResponse >
getWeatherSubscription = getWeatherUsecase.execute().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe(
smhiResponseModel -> {Log.d(TAG,"Retrieved weather"); locationView.hideLoading();},
err -> {Log.d(TAG,"Error fetching weather"); locationView.hideLoading();}
);
You don't return an observable from the callback but wrap your callbacks into observables to make them combinable (untested):
Observable<WeatherResult> weatherObservable = Observable.create(subscriber -> {
Awareness.SnapshotApi.getWeather(client)
.setResultCallback(weather -> {
if (!weather.getStatus().isSuccess()) {
subscriber.onError(new Exception("Could not get weather."));
Log.d(TAG, "Could not get weather");
} else {
//How do I do here?
subscriber.onNext(weather);
subscriber.onCompleted();
}
});
});
Observable<LocationResult> locationObservable = Observable.create(subscriber -> {
Awareness.SnapshotApi.getLocation(mGoogleApiClient)
.setResultCallback(retrievedLocation -> {
if(!retrievedLocation.getStatus().isSuccess()) {
subscriber.onError(new Exception("Could not get location."));
} else {
Log.d("FRAG", retrievedLocation.getLocation().getLatitude() + "");
subscriber.onNext(retrievedLocation);
subscriber.onCompleted();
}
});
});
now combine them via .combineLatest() or .zip():
Observable<CombinedResult> combinedResults = Observable.zip(weatherObservable, locationObservable,
(weather, location) -> {
/* somehow combine weather and location then return as type "CombinedResult" */
});
don't forget to subscribe, otherwise none of them gets executed:
combinedResults.subscribe(combinedResult -> {/*do something with that stuff...*/});
Observable.combineLatest(getWeather (), getLocation(), new Func2<List<Object_A>, List<Object_B>, Object>() {
#Override
public Object call(Object o, Object o2) {
combine both results and return the combine result to observer
}
})
getweather() and getlocation() return observables

is there better way using rxjava with multiple requests?

I'm new in rxjava or rxandroid, and looking for a better way dealing with multiple requests. I need to get the token from server and use the result as a parameter to do login verification and if it returns success then get the sessionId through getSessionId method.
I've considered about zip or merge, but I don't think it'll work. So can you give me an idea or I don know , train of thought?
Thank you.
Here's my code:
private void getToken(final String name , final String pwd){
api.newToken()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<TokenModel>() {
#Override public void call(TokenModel tokenModel) {
String token = tokenModel.request_token;
if (!"".equals(token)){
login(token, name, pwd);
}else {
Timber.e("got token failed");
}
}
});
}
private void login(String token, String name, String pwd){
api.validateToken(token, name, pwd)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<TokenModel>() {
#Override public void call(TokenModel tokenModel) {
String token = tokenModel.request_token;
if (!"".equals(token)){
getSessionId(token);
}else {
Timber.e("got token failed");
}
}
});
}
private void getSessionId(String token){
api.newSessionn(token)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<TokenModel>() {
#Override public void onCompleted() {
//go to home activity
}
#Override public void onError(Throwable e) {
//handle error
}
#Override public void onNext(TokenModel tokenModel) {
//store session id
}
});
}
Your first subscription call your second subscription, ...
You can avoid this using flapmap operator.
api.newToken(...)
.flapMap(token -> api.validateToken(token))
.flapMap(token -> api.newSession(token)).subscribe()
New observable in a subscription can offen be replaced by a flatMap call.
If you want to manage your error, in a flatMap, if the token is invalid, your can return an error observable instead of returning new api call observable.
.flatMap(token -> if(token.isValid){ return api.newCall(); } else { return Observable.error(...); ;)

Categories

Resources