rx-android

Where to draw the line with reactive programming [closed]

若如初见. 提交于 2019-12-05 03:27:54
I have been using RxJava in my project for about a year now. With time, I grew to love it very much - now I'm thinking maybe too much... Most methods I write now have some form of Rx in it, which is great! (until it's not). I now notice that some methods require a lot of work to combine the different observable producing methods. I get the feeling that although I understand what I write now, the next programmer will have a really hard time understanding my code. Before I get to the bottom line let me give an example straight from my code in Kotlin (Don't dive too deep into it): private fun <T

Filter list of objects in RxJava

放肆的年华 提交于 2019-12-05 01:23:26
I am trying to filter the list on the basis of it's property. For example, Sensors class has a property isActive and I want to get all the objects with isActive as true but I am unable to do it. I tried different ways but I didn't find the solution. Can someone help me to do it? Here is my code: mCompositeDisposable.add( fcService.getStationList() .subscribeOn(Schedulers.io()) .flatMap( stations -> { return fcService.getSensorList(stations.get(0).getName().getOriginal()); }) .subscribe(this::handleSensors, this::handleError) ); First, you need to emit each item from the List individually. That

RxJava/Android: Combine result of two dependent Observables

余生颓废 提交于 2019-12-05 01:09:42
I have two Observables . Observable<A> getAObservable() Returns Observable of A Observable<B> getBObservable(A) Returns Observable of 'B'. Here Observable<A> should execute before Observable<B> so that it can pass its result A to getBObservable() method. Now once Observable<B> completes I need to combine the result of these observable and Observable<AB> should returns. Options Tried: Take Observable<A> and apply flatMap on it so that it transform result in B . Now at this point I am not having access to A data. Hence I can't return Observable I can use zip so that once I get the result of both

RxJava timer that repeats forever, and can be restarted and stopped at anytime

丶灬走出姿态 提交于 2019-12-04 23:14:44
In android i use Timer to execute task that repeats every 5 seconds and starts after 1 second in this way: Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { // Here is the repeated task } }, /*Start after*/1000, /*Repeats every*/5000); // here i stop the timer timer.cancel(); this timer will repeat Until i call timer.cancel() I am learning RxJava with RxAndroid extension So i found this code on internet, i tried it and it doesnt repeat: Observable.timer(3000, TimeUnit.MILLISECONDS) .subscribeOn(Schedulers.newThread()) .observeOn

RxJava : How to handle error with zip operator ?

偶尔善良 提交于 2019-12-04 22:29:39
I am using RxJava and RxAndroid with Retrofit2. Observable<ResponseOne> responseOneObservable = getRetrofitClient().getDataOne() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); Observable<ResponseTwo> responseTwoObservable = getRetrofitClient().getDataTwo() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); Using zip operator as below on above two Observer. Observable<ArrayList<TestData>> testDataObservable = Observable.zip(responseOneObservable, responseTwoObservable, new Func2<ResponseOne, ResponseTwo, ArrayList<TestData>>() { @Override public

Downloading files using OkHttp, Okio and RxJava

杀马特。学长 韩版系。学妹 提交于 2019-12-04 21:57:09
I'm trying to download files using OkHttp and writing to disk with Okio. Also I've created an rx observable for this process. It is working, however it is noticeably slower than what I had previously used (Koush's Ion library). Here's how I create the observable: public Observable<FilesWrapper> download(List<Thing> things) { return Observable.from(things) .map(thing -> { File file = new File(getExternalCacheDir() + File.separator + thing.getName()); if (!file.exists()) { Request request = new Request.Builder().url(thing.getUrl()).build(); Response response; try { response = client.newCall

How can multiple requests used to populate models be handled using RXJava Observables?

我怕爱的太早我们不能终老 提交于 2019-12-04 21:47:06
问题 We are using ReactiveX and Retrofit in our network stack to handle all API requests in an asynchronous way. Our goal is to create one method that will return a completely populated collection of User models. Each User model has a list of Pet objects. We can fetch all of the User models with one request. However, Pet models need to be requested per User . Getting users is simple: // Service.java @GET("users/?locationId={id}") Observable<List<User>> getUsersForLocation(@Path("id") int

Difference between Observable.create() and Observable.fromCallable()

£可爱£侵袭症+ 提交于 2019-12-04 19:16:10
问题 Suppose we are getting a generic Object from SharedPrefs using .create() : return Observable.create(subscriber -> { String json = sharedPreferences.getString(key, ""); T myClass = gson.fromJson(json, generic); subscriber.onNext(myClass); subscriber.onComplete(); }); and using .fromCallable() : return Observable.fromCallable(() -> { String json = sharedPreferences.getString(key, ""); return gson.fromJson(json, generic); }); Is there any Difference if we call onComplete() immediately after

Rx 2 Android what is better Single or Observable for api calls?

岁酱吖の 提交于 2019-12-04 16:25:49
问题 when we use retrofit2 for doing API rest calls with Rx, What is the best approach to use, Single or Observable? public interface ApiService{ Single<Data> getDataFromServer(); Observable<Data> getDataFromServer(); } 回答1: I'd suggest using a Single as it is more accurate representation of the data flow: you make a request to the server and the you get either one emission of data OR an error: Single: onSubscribe (onSuccess | onError)? For an Observable you could theoretically get several

How to group by and return list in rx java

て烟熏妆下的殇ゞ 提交于 2019-12-04 16:17:03
I'm trying to create group list based on certain conditions in Rxjava. Below is my response: { "dates":[ { "date":18, "value":"1" }, { "date":18, "value":"2" }, { "date":18, "value":"3" }, { "date":19, "value":"1" }, { "date":19, "value":"2" }, { "date":19, "value":"3" }, { "date":19, "value":"4" } ] } How can group by the values 18 [value 1, value 2, value 3, highestvalue= 3, lowestvalue = 1] 19[value 1, value 2, value 3, value 4,highestvalue= 4, lowestvalue = 1] using Rxjava Note: I can create using for loop but the response will be fetched from the server and since it is returning