Last active
January 1, 2019 21:11
-
-
Save evandroferreiras/9caad1cfcc211be815e10d2af5f27132 to your computer and use it in GitHub Desktop.
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
interface PlacesService { | |
@GET("places/{id}") | |
Observable<Place> getPlaceById(@Path("id") int placeId); | |
} | |
interface UsersService { | |
@GET("users") | |
Observable<List<User>> listUsers(); | |
} | |
class Datasource { | |
public void getUsersWithPlace() { | |
userService.listUsers() | |
.flatMapIterable(users -> users) | |
.flatMap(user -> placesService.getPlaceById(user.placeId), | |
(user, place) -> user.name + " - " + place.title) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(new Observer<String>() { | |
ArrayList<String> dataSet = new ArrayList<String>(); | |
@Override | |
public void onSubscribe(Disposable d) { | |
disposables.add(d); | |
} | |
@Override | |
public void onNext(String s) { | |
dataSet.add(s); | |
} | |
@Override | |
public void onError(Throwable e) { | |
Log.e(TAG, "onError: ",e ); | |
} | |
@Override | |
public void onComplete() { | |
for (Disposable disposable : disposables ) { | |
if (!disposable.isDisposed()){ | |
disposable.dispose(); | |
} | |
} | |
updateAdapter(dataSet.toArray(new String[0])); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment