rx-java

How to handle pagination with Retrofit 2 and RxJava

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-03 17:19:29
问题 I know how to handle Retrofit responses but I have a problem with handling pagination from a REST API with rx java. Background The rest api that I'm using gives me the following response with the link to the next page in the headers: HTTP 200 OK Allow: GET, HEAD, OPTIONS Content-Type: application/json Link: <http://some-endpoint.com/pictures?page=2>; rel="next" Vary: Accept X-Total-Count: 80 [ { "id": 3, "url": "", "picture": { "id": 6, "url": "", "name": "Chrysler" }, "location": { "type":

Rx Buffer with timeout since first new group element

时光总嘲笑我的痴心妄想 提交于 2020-01-03 15:56:30
问题 pretty new to Rx world, and I need to implement the following behavior: I need the observable to gather values and emit them as a list once I have at least N items, or if a T amount of time passed since the first item of the group was emitted. I read the docs again and again, pretty sure it'll use buffer(timespan, unit, count[, scheduler]) But the issue is that the timespan here is depending on the last group of items. And if possible, I would also need to be able to flush (force the emission

Create an Observable that would accept arguments

佐手、 提交于 2020-01-03 11:33:20
问题 What is the proper way, if any, to create Observable s that are capable of accepting parameters? For instance, I could parameterize http requests 回答1: You can use Observable.create for that: public static Observable<String> createMyObservable(final String all, final Integer my, final Boolean parameters) { return new Observable.create(new Observable.OnSubscribe<String>(){ @Override public void call(Subscriber<? super String> subscriber) { // here you have access to all the parameters you

Create an Observable that would accept arguments

浪子不回头ぞ 提交于 2020-01-03 11:32:01
问题 What is the proper way, if any, to create Observable s that are capable of accepting parameters? For instance, I could parameterize http requests 回答1: You can use Observable.create for that: public static Observable<String> createMyObservable(final String all, final Integer my, final Boolean parameters) { return new Observable.create(new Observable.OnSubscribe<String>(){ @Override public void call(Subscriber<? super String> subscriber) { // here you have access to all the parameters you

How to use switchIfEmpty RxJava

杀马特。学长 韩版系。学妹 提交于 2020-01-03 11:28:49
问题 The logic here is that if the ratings in the database are empty, then I want to get them from the API. I have the following code: Observable.from(settingsRatingRepository.getRatingsFromDB()) .toList() .switchIfEmpty(settingsRatingRepository.getSettingsRatingModulesFromAPI()) .compose(schedulerProvider.getSchedulers()) .subscribe(ratingsList -> { view.loadRatingLevels(ratingsList, hideLocks); }, this::handleError); The getRatingsFromDB() call returns List<SettingRating> , but the API call

RxJava .subscribeOn(Schedulers.newThread()) questions

荒凉一梦 提交于 2020-01-03 04:01:11
问题 I am on plain JDK 8. I have this simple RxJava example: Observable .from(Arrays.asList("one", "two", "three")) .doOnNext(word -> System.out.printf("%s uses thread %s%n", word, Thread.currentThread().getName())) //.subscribeOn(Schedulers.newThread()) .subscribe(word -> System.out.println(word)); and it prints out the words line by line, intertwined with information about the thread, which is 'main' for all next calls, as expected. However, when I uncomment the subscribeOn(Schedulers.newThread(

Android MVVM - Update ViewModel when data changes

耗尽温柔 提交于 2020-01-02 23:45:34
问题 I'm working on an app using the MVVM pattern with RxJava . The architecture is the following: It's the first time i use this pattern and i'm not sure about the best way to update a ViewModel (and consequently the corresponding View ) when a change occurs in the data, made by another component of the application. For example: suppose we have an Activity showing the list of users i follow (like a social app), from this list i select a user and open his profile in another Activity . Now, from

How to combine Retrofit 2 with Realm and RxJava

血红的双手。 提交于 2020-01-02 10:04:11
问题 I want to save retrofit responses to realm on the background thread then pass it to the UI Thread, but its a bit tricky since Realm is very touchy with threads. so the code would look like something like this, please submit your edits to all better solutions :) restApi.userRealmList() .doOnNext(userRealmModels -> { if (userRealmModels != null){ mRealm = Realm.getInstance(mContext); mRealm.asObservable() .map(realm -> mRealm.copyToRealmOrUpdate(userEntity)) .subscribe(new Subscriber<Object>()

Combine Observable with second Observable which uses results from from the first one

Deadly 提交于 2020-01-02 05:23:21
问题 I have two methods returning Observable: Observable<String> firstObservable(); Observable<String> secondObservable(String value); For each result from the first Observable I get new instance of the second Observable. For each result from the second observable I would return object with combined results. firstObservable -> x----x----x----x----x \ \ \ \ \ secondObservable -> y(x)-y(x)-y(x)-y(x)-y(x) \ \ \ \ \ result -> {x,y}-{x,y}-{x,y}-{x,y}-{x,y} How can this be done? 回答1: There's an

Calling network services in parallel using RxJava. Is this the right way?

我怕爱的太早我们不能终老 提交于 2020-01-02 04:37:05
问题 Idea is to make 3 network calls in parallel. (I am using Google as the servies for demo purpose. The following works but not sure if this is the right way or it can be simplified. What should I do if I have to combine the responses of all the three searches? Please advise. public class GoogleSearchRx { public static void main(String args[]) { CountDownLatch latch = new CountDownLatch(3); search("RxJava").subscribeOn(Schedulers.io()).subscribe( links -> { links.forEach(link -> out.println