Last active
July 16, 2021 02:12
-
-
Save rohans-doordash/6daffe76b538bea24ee5989f6875ef0d to your computer and use it in GitHub Desktop.
Fetch restaurants at a user's address by using RxJava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Single<List<Restaurant>> getRestaurants(int userId) { | |
return ddApi.getUserInfo(userId).flapMap(user -> { | |
return ddApi.getAvailableRestaurants(user.defaultAddress.lat, user.defaultAddress.lng); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RestaurantFragment { | |
private CompositeDisposables disposables = new CompositeDisposables(); | |
private RestaurantDataSource restaurantDataSource; | |
@Override | |
public void onResume() { | |
// subscribe to the Single returned by RestaurantApi | |
restaurantDataSource | |
.getRestaurants(userId) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new SingleObserver<Restaurant>() { | |
@Override | |
public void onSubscribe(Disposable d) { | |
disposables.add(d); | |
} | |
@Override | |
public void onSuccess(List<Restaurant> restaurants) { | |
// update the adapter with restaurants | |
} | |
@Override | |
public void onError(Throwable e) { | |
// display an error message | |
} | |
}); | |
} | |
@Override | |
public void onPause() { | |
disposables.clear(); | |
} | |
} |
I think it must be CompositeDisposable not CompositeDisposable's'.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nit: it should be flatMap, not flapMap