rx-java

RxJava: combine two optional observables

烈酒焚心 提交于 2021-02-19 04:09:14
问题 I have two Observable s, let's call them PeanutButter and Jelly . I'd like to combine them to a Sandwich Observable . I can do that using: Observable<PeanutButter> peanutButterObservable = ...; Observable<Jelly> jellyObservable = ...; Observable<Sandwich> sandwichObservable = Observable.combineLatest( peanutButterObservable, jellyObservable, (pb, j) -> makeSandwich(pb, j)) The problem is that RX waits for the first PeanutButter and the first Jelly to be emitted before emitting the first

RxJava: combine two optional observables

守給你的承諾、 提交于 2021-02-19 04:08:20
问题 I have two Observable s, let's call them PeanutButter and Jelly . I'd like to combine them to a Sandwich Observable . I can do that using: Observable<PeanutButter> peanutButterObservable = ...; Observable<Jelly> jellyObservable = ...; Observable<Sandwich> sandwichObservable = Observable.combineLatest( peanutButterObservable, jellyObservable, (pb, j) -> makeSandwich(pb, j)) The problem is that RX waits for the first PeanutButter and the first Jelly to be emitted before emitting the first

Why is RxJava often used with Retrofit?

谁说我不能喝 提交于 2021-02-17 08:36:50
问题 What is the benefit of using Retrofit in combination with Rxjava? 回答1: Question Retrofit Already in run on background thread. Then why need another background task RxJava? I think most importanly, avoid nested callbacks( callback hell ). e.g) Callback hell (Retrofit) public interface MyService { @GET("users") Call<List<UserModel>> getUser(); @GET("userinfo") Call<UserInfoModel> getUserInfoById(@Query("id") Integer id); } service.getUser().enqueue(new Callback<UserModel>() { @Override public

Passing List to RxJava Concat and when remove item in onNext or onError causes ConcurrentModificationException

好久不见. 提交于 2021-02-15 07:42:15
问题 I have this code which I can use with concat but what I need is a way of passing Iterator so that I can add or remove items which isn't possible with this code. I want to be able to remove and add items to concat without causing java.util.ConcurrentModificationException I need to do this for different purpose such as if some items are success remove them and retry others. Or if token is invalid then remove all and refresh token and add all with modified token. Observable userObservable =

Is It a Good Idea to Make Singleton's getInstance() Method Asynchronous by Making It Returns an Observable<Singleton>?

随声附和 提交于 2021-02-11 15:25:12
问题 I have a singleton that takes a few seconds to instantiate. It makes the UI freezes. So I'm planning to make the getInstance() method asynchronous. Is writing the following code a common practice? /* * The singleton class */ public class Singleton { private static volatile Singleton instance; public static Observable<Singleton> getInstance(Context context) { return Observable.fromCallable(() -> { synchronized (Singleton.class) { if (instance == null) instance = new Singleton(context

Making around 20 HTTP calls and passing data to the database using Java

試著忘記壹切 提交于 2021-02-11 07:07:08
问题 I have a collection of 20 items, I will create a loop for the items and make API Calls to get the data, based on the data returned, I will have to update in the database. This requirement is simple and I am able to accomplish in plain Java. Now for performance, I am learning about using RxJava . I went through many articles in the internet and found that people refer to the async-http-client library for async http calls, I find that the library is out of date and the maintainer is planning

Making around 20 HTTP calls and passing data to the database using Java

久未见 提交于 2021-02-11 07:03:24
问题 I have a collection of 20 items, I will create a loop for the items and make API Calls to get the data, based on the data returned, I will have to update in the database. This requirement is simple and I am able to accomplish in plain Java. Now for performance, I am learning about using RxJava . I went through many articles in the internet and found that people refer to the async-http-client library for async http calls, I find that the library is out of date and the maintainer is planning

RxJava tricky startWith(Observable)

三世轮回 提交于 2021-02-10 05:51:47
问题 The following code emits items from observable1 only after observable2 is completed. observable1.startWith(observable2) .subscribe() I need to achieve another behavior observable1 -> 0 1 2 3 observable2 -> 1 2 3 4 5 6 observable1.startWithDefault(observable2) -> 1 2 0 1 2 3 The second observable emits items only while first observable is empty and then items from first one are emited. I could not find correct solution using only basic operators, what is correct RxJava 2 implementation of

Naming convention for methods returning RxJava's Completable

你离开我真会死。 提交于 2021-02-08 12:40:18
问题 I have and Android app with the view class ( Fragment , Activity ) observing its ViewModel . The ViewModel exposes methods such as getUserName which returns Observable<String> . Although maybe there is a possibility to find a better name (maybe observeUserName ), I'm happy with the current one - it is quite explanatory. However, here starts the hard part: ViewModel also can tell the view to perform some operation - for example close itself, pop backstack etc. For this case ViewModel defines

How to handle multiple data sources with rxjava?

社会主义新天地 提交于 2021-02-08 03:46:53
问题 This is the case: I have the domain layer to provide data fetching interface for business logics, and I have 2 data sources: local database and remote network. It works like this: Request all users: DataRepository.getInstance().getUsers(); In DataRepository, there are 2 sources: LocalDataSource.getUsers() which fetches all users from local database, if there is no data then ignore this request. RemoteDataSource.getUsers() which requests latest user list from our server(Even if there is data