rx-java2

Handling Error RXJava Android with Kotlin

你说的曾经没有我的故事 提交于 2019-12-05 07:12:20
Hi I'm new with RxJava and Kotlin and I loose some concepts about it. I have "api" like this: interface VehiclesService { @GET("/vehicles/") fun getVehicles(): Single<List<Vehicle>> } Then I create the retrofit client, etc.. like this: var retrofit = RetrofitClient().getInstance() vehiclesAPI = retrofit!!.create(VehiclesService ::class.java) finally I do the call: private fun fetchData() { compositeDisposable.add(vehiclesAPI .getVehicles() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe { vehicles -> displayData(vehicles) } ) } And here is where I have the

Rxjava 2 exception with camera

∥☆過路亽.° 提交于 2019-12-05 05:46:52
I just switched my code from asynctask to rxjava2 and I'm randomly getting this exception on my nexus: Camera is being used after Camera.release() was called in Galaxy s6 Edge Following is my code- Class Camera : public class Cameras { private static final String TAG = Cameras.class.getSimpleName(); private static final String SP_CAMERA_ID = "camera_id"; private static final int NO_NEXT_TASK = 0; private static final int NEXT_TASK_RELEASE_COMPLETE = 1; private static final int NEXT_TASK_SWITCH_COMPLETE = 2; private static final int NEXT_TASK_START_PREVIEW = 3; private Camera camera; private

Executing delete with room (rxjava)

孤者浪人 提交于 2019-12-05 04:08:01
In room the @Delete annotation doesn't emit anything. This is what the dao looks like @Dao public interface UserDao { @Delete void deleteUser(User user); //We can't use Maybe or Single or anything here } This makes it a problem while doing something like userRepository.deleteUser().subscribeOn since we have no emission coming the dao . I use the following code to call deleteUser on a background thread. Observable.just(appDatabase). subscribeOn(SchedulerProvider.getInstance().computation()). subscribe(db -> { userRepository.logoutUser(loggedUser.getLoggedInUser()); loggedUser.setLoggedInUser

RxJava Subject with Backpressure - only let the last value emit once downstream has finished consuming

↘锁芯ラ 提交于 2019-12-05 02:24:34
问题 I have a PublishSubject that calls onNext() on some UI event. The subscriber usually takes 2 seconds to complete its job. I need to ignore all calls to onNext() except the last one while the subscriber is busy. I tried the following, however I'm unable to control the flow. The requests seem to get queued up and each and every request gets processed (and so back pressure isn't seemingly working). How can I make it ignore all requests but the last one? (I don't want to use debounce as the code

PublishSubject with backpressure in RxJava 2.x

混江龙づ霸主 提交于 2019-12-05 01:46:56
I am currently choosing between RxJava 1.x or 2.x for my current project. I basically need a PublishSubject with a backpressure strategy onBackpressureLatest() . I want to choose RxJava 2.x, but i can't quite get my head around on how to apply a backpressure strategy to a PublishSubject , as it inherits from Observable and not from Flowable . Could you please tell me how to create a PublishSubject with a onBackpressureLatest() backpressure strategy in RxJava 2.x ? In 2.x the backpressure was moved to the base type Flowable and its hot partners PublishProcessor, ReplayProcessor etc.

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

How to continue processing after an error happens in RxJava 2?

大兔子大兔子 提交于 2019-12-04 23:41:38
问题 I have a PublishSubject and a Subscriber which I use to process a (possibly) infinite stream of preprocessed data. The problem is that some of the elements might contain some error. I'd like to ignore them and continue processing. How can I do so? I've tried something like this: val subject = PublishSubject.create<String>() subject.retry().subscribe({ println("next: $it") }, { println("error") }, { println("complete") }) subject.onNext("foo") subject.onNext("bar") subject.onError

How to convert a List<Object> to PagedList<Object> and vice-versa?

ぐ巨炮叔叔 提交于 2019-12-04 23:06:48
PagedList<Object> is used for Android's cool paging library. To make the question as minimal as possible : If i have a list of strings like List<String> stringList; // it consists of 200 strings I want to convert stringList to type PagedList<String> like PagedList<String> pagedStringList; And also if i have a PagedList<Object> how can convert it to a List<Object> ? I went through this for reference If i try the other way round .... How can I convert List<Object> into DataSource.Factory<Integer, Object> ..so that indirectly i can convert it into PagedList<> ? From DataSource.Factory<Integer,

RxJava 2, what happen the resultSelector argument of flatmap?

感情迁移 提交于 2019-12-04 19:46:04
In RxJava1 flatmap had a overloaded method that allowed you to retain source values and pass it down the stream. I've gained this knowledge from the following blog post https://medium.com/rxjava-tidbits/rxjava-tidbits-1-use-flatmap-and-retain-original-source-value-4ec6a2de52d4 However, moving to RxJava2, I cannot seem to find it. I checked the changes from Rx1 and Rx2 and it is not listed. I would like to know if it exists still but I am perhaps not looking in the right place. I am using a Single by the way. I don't think Single ever supported this operator and the Observable / Flowable

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