I am new to Rx and I've been wondering how to properly emit values one by one (lazy load images in this example). What is the best way to do this without encountering concurrency issues?
This is my code:
@Override
public Observable<Bitmap> getPhotos(final String placeId) {
return Observable.create(new ObservableOnSubscribe<Bitmap>() {
@Override
public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
try {
PlacePhotoMetadataResult res = Places.GeoDataApi
.getPlacePhotos(googleApiClient, placeId)
.await();
PlacePhotoMetadataBuffer buffer = res.getPhotoMetadata();
for (PlacePhotoMetadata photoMetadata : buffer) {
Bitmap photo = photoMetadata
.getPhoto(googleApiClient)
.await()
.getBitmap();
emitter.onNext(photo);
}
buffer.release();
} catch (Exception ex) {
emitter.onError(ex);
} finally {
emitter.onComplete();
}
}
});
}
Thanks!