rx-java

If it safe to have blocking operation inside flatMap { … } mapper function?

不羁的心 提交于 2019-12-24 17:52:37
问题 I'd like to organize a thread barrier: given a single lock object, any thread can obtain it and continue thread's chain further, but any other thread will stay dormant on the same lock object until the first thread finishes and releases the lock. Let's express my intention in code (log() simply prints string in a log): val mutex = Semaphore(1) // number of permits is 1 source .subscribeOn(Schedulers.newThread()) // any unbound scheduler (io, newThread) .flatMap { log("#1") mutex

Retrofit + rxJava: how to implement iterable N requests?

為{幸葍}努か 提交于 2019-12-24 15:56:40
问题 I have a problem to implement following problem: I'm making a request that fetches all active leagues. Then, for each of them I need to make another request to grab the matches. I think it's possible to implement the solution using flatMapIterable, but don't know how. For now I have following retrofit interfaces: public interface LeaguesApi { @GET("/api/get-leagues") Observable<ArrayList<League>> getLeagues(@Query("active_only") boolean activeOnly); } public interface LeagueApi { @GET("/api

Filter sublist using RxJava

半城伤御伤魂 提交于 2019-12-24 13:31:22
问题 Using the following objects : Order { int orderId; List<Item> items; } Item { int price; String description; boolean free; } The goal is to keep non-free items starting from an Observable<0rder> and still return an Observable<0rder>. I'm doing the following for now, but my items doesn't get filtered : getMyOrder() // returns Observable<Order> from the network .subscribeOn(Schedulers.io()) .observeOn(Schedulers.computation()) .flatMap( order -> Observable.from(order.items) .filter(item ->

How to pass custom pojo as parameter to the interface method call in rxjava2 Observable?

半腔热情 提交于 2019-12-24 12:18:23
问题 Current code Observable.from(listMovie)//list of movie .flatMap(new Func1<Movie, Observable<FavMovieRes>>() { @Override public Observable<FavMovieRes> call(Movie movie) { return moviesAPI.makeMovieFav(userId), sessionId, new MakeMovieFav("movie", movie.getId(), movie.isFavList())); } }) .subscribe(new Subscriber<FavMovieRes>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(FavMovieRes favMovieRes) { } }); In above code I

How RXJava Scheduler/Threading works for different operator?

爷,独闯天下 提交于 2019-12-24 11:57:25
问题 Can anyone please help me to explain which scheduler are running below code? Completable.complete() .subscribeOn(http://Schedulers.io ()) .observeOn(AndroidSchedulers.mainThread()) .delay(5000, TimeUnit.MILLISECONDS) .doOnComplete(() -> liveDataState.postValue("")) .subscribe() My question is which schedulers are delay(), doOnComplete() and subscribe() are using io or mainThread ? 回答1: After digging into RxJava threading last two days found the rule of thumbs for handling RxJava Threading

How to create an observable sequence where part of it can be unsubscribed and resubscribed to?

假装没事ソ 提交于 2019-12-24 06:38:15
问题 I have a fairly complex feature I want to implement reactively. Whether this feature is enabled depends on the following User preference for this feature has been enabled (user can toggle this feature on or off) Activity is started ( Activity#onStart() ). If you aren't familiar with Android, this is just means the view is visible to the user. Here are the following observables I already have. Observable<Boolean> - a user preference for whether this feature is enabled. true if enabled, false

What will happen if every request has a retrofit instance?

浪子不回头ぞ 提交于 2019-12-24 05:51:58
问题 I was thinking about a question recently. If every request has a retrofit instance,What will happen? The reason I want every request has a retrofit instance: Every retrofit instance has an OkHttpClient instance ,so I want add Interception to OkhttpClient,but not every request should be intercepted ,and I also want to add some same headers to OkHttpClient,but not every request must has these same headers ,like when it's login request,I don't need add token.,but other request may need. So my

in 0.18, how do background tasks get executed on a scheduler?

百般思念 提交于 2019-12-24 04:43:17
问题 Feels like I'm missing something here, but where I used to do: Schedulers.io().schedule(new Action1<Scheduler.Inner>() { @Override public void call(Scheduler.Inner inner) { doWhatever(); } }); I don't seem to be able to simply use a scheduler to run a background task anymore, without managing unsubscribes (https://github.com/Netflix/RxJava/wiki/Scheduler and https://github.com/Netflix/RxJava/blob/master/rxjava-core/src/main/java/rx/Scheduler.java). Is there a pattern for 0.18 that allows me

RxJava. Read file to observable

那年仲夏 提交于 2019-12-24 01:31:48
问题 I am totally new to RxJava and reactive programming. I have an assignment where i must read file and store it to Observable. I have tried to make a Callable with BufferedReader inside and use Observable.fromCallable(), but it didn't work much. Could you please show me how can i do that? I am using RxJava 2.0. 回答1: A basic solution where I use a nested class FileObservableSource to produce the data and then defer the creation of the Observable until an Observer subscribes: import io.reactivex

RxJava: Unable to create call adapter for rx.Observable error

喜欢而已 提交于 2019-12-24 01:04:21
问题 What i do: public interface ApiInterface { @Multipart @POST("/android/upload/index.php") Observable<Response> postImage(@Part MultipartBody.Part image, @Part("name") RequestBody name); } Model: public interface Model { Observable<Response> postImage(MultipartBody.Part image, RequestBody name); } Impl: public class ModelImpl implements Model { ApiModule apiModule = ApiModule.getInstance(); @Override public Observable<Response> postImage(MultipartBody.Part image, RequestBody name) { return