rx-java

How to invoke a method for each element in a collection parallely using RxJava

天大地大妈咪最大 提交于 2019-12-22 12:45:07
问题 I have below two methods and need to invoke findDetails(name) method parallely for all names in the list. How can achieve this using RxJava? Basically I want to invoke findDetails method for each element in teh collection in diffrent threads. public List<Bean> findStudentDetails(List<String> names){ names.forEach(s->{ findDetails(s); }); } private Bean findDetails(String name){ //some io operation } 回答1: Make your methods to return Observable , then flatMap your Observables with applied

Migrate from RxAndroid 1.x to 2.x (RxJava included)

不想你离开。 提交于 2019-12-22 12:23:52
问题 I have a project running RxAndroid 1.x (everythings works). I am trying to migrate to 2.x version. My gradle file: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support:design:24.2.1' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' //compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0' compile 'com.squareup.okhttp3:okhttp:3.4.1' compile 'com.squareup

NetworkOnMainThreadException in concatMap() with Retrofit 2 and RxJava

泄露秘密 提交于 2019-12-22 11:37:07
问题 In my app I'm trying to use RxJava to react when the user search for a new movie and retrieve movie data from a webservice. In order to do this I thought to use concatMap to concatenate the action of getting the new movie query to requesting the movie to the webservice. Doing this I get a NetworkOnMainThread exception and I don't understand the reason.. createSearchViewObservable(searchView) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .concatMap(new Func1<String,

How do I handle response errors using with Retrofit and RxJava/RxAndroid?

我与影子孤独终老i 提交于 2019-12-22 10:59:46
问题 I am having trouble working out how to handle a response error with retrofit and RxAndroid. onError() gets called if there is a network error or the like but I need to be able to get the response to check if there was an authentication error. Instead what I get is a token with a null String and I can't find out why. What is the best way to go about this? This is my RxAndroid call at the moment. Client.getInstance().getService() .getToken(usernameET.getText().toString(), passwordET.getText()

How can I reuse a Subscriber between two Observables (RxJava)

柔情痞子 提交于 2019-12-22 08:47:22
问题 In order to not repeat myself, I want to re-use a Subscriber variable between two observables. How do you do accomplish this? My current code below does not work, because after the subscriber is used once, it is unsubscribed and no longer works again. If I new a Subscriber instead of reusing a variable, my subscription works. I don't want to write the same code twice, if possible. public class HomePresenter extends BasePresenter<HomeView> { ArticleRepo articleRepo; @Inject public

Combining timeout() with retryWhen()

六月ゝ 毕业季﹏ 提交于 2019-12-22 06:55:55
问题 I'm creating a simple app for connecting with the bluetooth devices using RxAndroidBle library (Cheers Guys for great work!). What I'm experiencing is sometimes when I connect to the device I receive the Gatt error with status 133. I know it may happen so what I want to do is retry everything when that error occurs. It's not a problem, I can easily do that with retryWhen() operator, however I have another requirement - the stream has to terminate after 30 seconds if the connection wasn't

Limit throughput with RxJava

↘锁芯ラ 提交于 2019-12-22 05:49:45
问题 The case I'm into right now is quite hard to explain so I will write a simpler version just to explain the issue. I have an Observable.from() which emits a sequence of files defined by an ArrayList of files. All of these files should be uploaded to a server. For that I have an function that does the job and returns an Observable . Observable<Response> uploadFile(File file); When I run this code it gets crazy, the Observable.from() emits all of the files and they are uploaded all at ones, or

Generate infinite sequence of Natural numbers using RxJava

做~自己de王妃 提交于 2019-12-22 05:29:14
问题 I am trying to write a simple program using RxJava to generate an infinite sequence of natural numbers. So, far I have found two ways to generate sequence of numbers using Observable.timer() and Observable.interval() . I am not sure if these functions are the right way to approach this problem. I was expecting a simple function like one we have in Java 8 to generate infinite natural numbers. IntStream.iterate(1, value -> value +1).forEach(System.out::println); I tried using IntStream with

How to emit items from a list with delay in RxJava?

别说谁变了你拦得住时间么 提交于 2019-12-22 04:46:10
问题 I'm using Retrofit to get bookmarks from REST API: public interface BookmarkService { @GET("/bookmarks") Observable<List<Bookmark>> bookmarks(); } Now I would like to emit each item from this list with delay. I did something similar to this in Java, but onCompleted is never triggered. private Observable<Bookmark> getBookmarks() { return getBookmarkService().bookmarks() .flatMap(new Func1<List<Bookmark>, Observable<Bookmark>>() { @Override public Observable<Bookmark> call(List<Bookmark>

RxJava takeUntil with emmision of last item?

谁都会走 提交于 2019-12-22 04:06:12
问题 Is there a possibility to emit item that meets condition in takeUntil operator? 回答1: Mmmm not sure if I understand your question. Something like this? @Test public void tesTakeUntil() { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); Observable.from(numbers) .takeUntil(number -> number > 3) .subscribe(System.out::println); } it will print 1 2 3 4 You can see more examples of Take here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/filtering/ObservableTake