Kotlin extension function on subsbcribing to RxJava's Flowable data - android

I want to write a function that automatically subsbcribes to RxJava's Flowable<T> and get the resulting data. This data will then be passed as an argument to another method that does the processing. I am struggling with Kotlin's extension function syntax and generics.
I want to convert this call:
val scheduler = Schedulers.newThread()
disposable.add(
viewModel.getExams().subscribeOn(scheduler)
.observeOn(scheduler)
.subscribe({ exams ->
exams.forEach {
getSubjectOfExam(it, Schedulers.newThread())
}
}, { error ->
Log.e(
"OverviewFragment",
"Unable to fetch list, $error"
)
})
)
which is very lengthy in my Activity code, to a method that returns the data that I want to process.
In this case I'd like a list of exams (List<Exam>) passed into the argument of getSubjectOfExam(), which is the method for the list processing.
My function so far, which compiles but does not work at all:
/**
* General subscription of items in a Flowable list
* #param f method to be executed when list is loaded
* #param scheduler scheduling units
*/
private fun Flowable<out List<Any>>.listSubscribe(
f: (List<Any>) -> Unit,
scheduler: Scheduler
) {
disposable.add(
this.subscribeOn(scheduler)
.observeOn(scheduler)
.subscribe({
f(it)
}, { error ->
Log.e(
"OverviewFragment",
"Unable to fetch list, $error"
)
})
)
}
it will be called like so:
viewModel.getExams().listSubscribe({ resultData ->
resultData.forEach {
val exam = it as Exam
getSubjectOfExam(exam, Schedulers.newThread())
}
}, Schedulers.newThread())
So yeah, I tried to make an extension function and passing a function as one of its arguments (called a higher-order function I believe).
With my method, the getSubjectOfExam doesn't get called at all. Is there something I'm missing?
I'll be subscribing to Flowable's all the time in my Activity so this function will really help me.

I tried your code and it seems it is working okay. Is there any chance that viewModel.getExams() or getSubjectOfExam() is not working?
Also I could suggest few optimizations:
protected fun <T> Flowable<out List<T>>.listSubscribe(
f: (List<T>) -> Unit,
scheduler: Scheduler
) {
disposable.add(
this.subscribeOn(scheduler)
.observeOn(scheduler)
.subscribe(f, { error ->
Log.e(
"OverviewFragment",
"Unable to fetch list, $error"
)
})
)
}
Then you won't need type conversion:
viewModel.getExams().listSubscribe({ resultData ->
resultData.forEach {
getSubjectOfExam(exam, Schedulers.newThread())
}
}, Schedulers.newThread())
In fact you can replace List<T> with just T and make it work with any types. Also, observing and subscribing with same scheduler doesn't make a lot of sense to me. I think you can remove.observeOn completely and the code will still observe on the same scheduler you put in .subscribeOn

Related

Room RxJava observable triggered multiple times on insert

I'm having a weird problem with my repository implementation. Every time I call my function that's supposed to get data from the database and update the database with a network call, I receive multiple results from my database observer.
override fun getApplianceControls(
serialNumber: SerialNumber
): Flowable<ApplianceControlState> {
val subject = BehaviorProcessor.create<ApplianceControlState>()
controlsDao.get(serialNumber.serial)
.map { controls ->
ApplianceControlState.Loaded(controls.toDomainModel())
}
.subscribe(subject)
controlApi.getApplianceControls(serialNumber.serial)
.flatMapObservable<ApplianceControlState> { response ->
val entities = response.toEntity(serialNumber)
// Store the fetched controls on the database.
controlsDao.insert(entities).andThen(
// Return an empty observable because the db will take care of emitting latest values.
Observable.create { }
)
}
.onErrorResumeNext { error: Throwable ->
Observable.create { emitter -> emitter.onNext(ApplianceControlState.Error(error)) }
}
.subscribeOn(backgroundScheduler)
.subscribe()
return subject.distinctUntilChanged()
}
#Dao
interface ApplianceControlsDao {
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(controls: List<TemperatureControlEntity>): Completable
#Query("SELECT * FROM control_temperature WHERE serial = :serial")
fun get(serial: String): Flowable<List<TemperatureControlEntity>>
}
Basically, if I call getApplianceControls once, I get desired result. Then I call again, with another serial number, which is empty and I get the empty array. But then I call a third time, but with the same serial number as the first time and I get a mix of correct results and empty array after the insert call is made.
Like this:
1st call, to serial number "123" -> Loaded([control1, control2, control3])
2nd call, to serial number "000" -> Loaded([])
3rd call, to serial number "123" -> Loaded([control1, control2, control3]), Loaded([]), Loaded([control1, control2, control3])
If I remove the db insert from the api response, it works fine. Everything weird occurs after insert is called.
Edit: getApplianceControls() is called from the ViewModel.
fun loadApplianceControls(serialNumber: SerialNumber) {
Log.i("Loading appliance controls")
applianceControlRepository.getApplianceControls(serialNumber)
.subscribeOn(backgroundScheduler)
.observeOn(mainScheduler)
.subscribeBy(
onError = { error ->
Log.e("Error $error")
},
onNext = { controlState ->
_controlsLiveData.value = controlState
}
).addTo(disposeBag)
}
As i mention in comment you have 2 subscriptions that are not unsubscribed anywhere, it could cause memory leak (it doesn't dispose when subject is disposed), also with such implementation you ignore API errors.
i'd try to change it to:
override fun getApplianceControls(serialNumber: SerialNumber): Flowable<ApplianceControlState> {
val dbObservable = controlsDao.get(serialNumber.serial)
.map { controls ->
ApplianceControlState.Loaded(controls.toDomainModel())
}
val apiObservable = controlApi.getApplianceControls(serialNumber.serial)
.map { response ->
val entities = response.toEntity(serialNumber)
// Store the fetched controls on the database.
controlsDao.insert(entities).andThen( Unit )
}
.toObservable()
.startWith(Unit)
return Observables.combineLatest(dbObservable, apiObservable) { dbData, _ -> dbData }
// apiObservable emits are ignored, but it will by subscribed with dbObservable and Errors are not ignored
.onErrorResumeNext { error: Throwable ->
Observable.create { emitter -> emitter.onNext(ApplianceControlState.Error(error)) }
}
.subscribeOn(backgroundScheduler)
//observeOn main Thread
.distinctUntilChanged()
}
I'm not sure if it solves the original issue. But if so - the issue is in flatMapObservable
ALSO would be useful to see controlApi.getApplianceControls() implementation.

How to add the body of the subscribe method

In the below code, I am trying to add the body for the .subscribe(). I tried to add the lambda notation but it never worked. Would you please tell me how to implement the .subscribe() method?
Given that, the setupCommRequestService() returns Single<..>
code:
setupCommRequestService()?.
flatMap {
it.getAllPhotos()
.map {
Observable.fromIterable(it)
.map {
it
}
}
.toSortedList()
}
?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(
)
There are 4 implementations for subscribe method according Single documentation. In a simple approach, you should implement a strategy for both onSucess and onError. therefor you should use the subscribe method either by passing a BiConsumer or 2 Consumer one for onSucess case and one for onError.
using BiConsumer in lambda:
val disposable = Single.just(1)
.subscribe { success, failure ->
/* whichever is not null */
}
or using 2 Consumer in lambda:
val disposable = Single.just(1)
.subscribe({ success ->
/* success */
}, { failure ->
/* failure */
})

Retrofit + Debouce on EditText is causing an InterruptedIOException

I currently have an EditText for the user to enter a search. I'm trying to use RxJava with debounce to only search every so often, instead of each character. However, I'm getting an InterruptedIOException while I'm testing, which kills the stream.
private val subject = BehaviorSubject.create<String>()
init {
configureAutoComplete()
}
private fun configureAutoComplete() {
subject.debounce(200, TimeUnit.MILLISECONDS)
.flatMap {
getSearchResults(query = it)
}
.subscribe({ result ->
handleResult(result)
}, { t: Throwable? ->
Logger.e(t, "Failed to search")
})
}
fun getSearchResults(query: String): Observable<List<MyObject>> {
val service = NetworkService.create() // get retrofit service
return service.search(query)
}
fun search(text: String) {
subject.onNext(text)
}
As you can see, I'm creating a BehaviorSubject, and within init I'm setting it up with debounce.
getSearchResult returns an Observable and does my network request.
But as I'm testing, if I type at a specific rate ( usually quick-ish, like typing another character while the request is ongoing ) it'll throw an Exception.
Failed to search : java.io.InterruptedIOException
at okhttp3.internal.http2.Http2Stream.waitForIo(Http2Stream.java:579)
at okhttp3.internal.http2.Http2Stream.takeResponseHeaders(Http2Stream.java:143)
at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:125)
I was looking at this, https://stackoverflow.com/a/47276430/3106174, and it seems like I'm doing everything correctly.
After more testing, I realized that the network request was on the main thread.
You can test this by replacing your network call with Observerable.create{ ... } and throwing a Thread.sleep(1000) inside.
I was following this tutorial, https://proandroiddev.com/building-an-autocompleting-edittext-using-rxjava-f69c5c3f5a40, and one of the comments mention this issue.
"But I think one thing is misleading in your code snippet, and it’s
that subjects aren’t thread safe. And the thread that your code will
run on will be the thread that you emitting on (in this case the main
thread). "
To solve this issue, you need to force it to run on Schedulers.io(). Make sure it's after the debounce or it won't work.
private fun configureAutoComplete() {
subject.debounce(200, TimeUnit.MILLISECONDS)
.observeOn(Schedulers.io()) // add this here
.distinctUntilChanged()
.switchMap {
getSearchResults(query = it)
}
.subscribe({ result ->
handleResult(result)
}, { t: Throwable? ->
Logger.e(t, "Failed to search")
})
}

RxJava objects taking too much time to be initialized

I'm using such construction to handle multiple data loading responses. The problem in that this code affects on fragment start time (+2 about second). load() function is placed into ViewModel. Fragment#onResume() method calls ViewModel#load() method. How can I optimize this code to reduce Fragment startup time?
fun load() {
Single.zip(
apiDataManager1.getSingle().subscribeOnIo().observeOnMain(),
apiDataManager2.getSingle().subscribeOnIo().observeOnMain(),
apiDataManager3.getSingle().subscribeOnIo().observeOnMain(),
Function3<Any, Any, Any, Unit> { _, _, _ ->
// process data
}
)
.subscribeOnIo()
.observeOnMain()
.subscribe()
}
UPD
If I wrap this function into Thread {} everything works fine. How can I do this only with Rx?
try start the chain with something else, specify observer. not sure it is inteneded but combining data in Function3 probably shouldn't be deliveded to main thread:
Single.defer(() -> Single.just(true))
.observeOn(Schedulers.io())
.flatMap( any -> Single.zip(
apiDataManager1.getSingle().subscribeOnIo(),
apiDataManager2.getSingle().subscribeOnIo(),
apiDataManager3.getSingle().subscribeOnIo(), { _, _, _ ->
// process data
})).subscribeOnIo()
.observeOnMain()
.subscribe()

Kotlin Coroutine to escape callback hell

I'm trying to use Kotlin's coroutines to avoid callback hell, but it doesnt look like I can in this specific situation, I would like some thougths about it.
I have this SyncService class which calls series of different methods to send data to the server like the following:
SyncService calls Sync Student, which calls Student Repository, which calls DataSource that makes a server request sending the data through Apollo's Graphql Client.
The same pattern follows in each of my features:
SyncService -> Sync Feature -> Feature Repository -> DataSource
So every one of the method that I call has this signature:
fun save(onSuccess: ()-> Unit, onError:()->Unit) {
//To Stuff here
}
The problem is:
When I sync and successfully save the Student on server, I need to sync his enrollment, and if I successfully save the enrollment, I need to sync another object and so on.
It all depends on each other and I need to do it sequentially, that's why I was using callbacks.
But as you can imagine, the code result is not very friendly, and me and my team starting searching for alternatives to keep it better. And we ended up with this extension function:
suspend fun <T> ApolloCall<T>.execute() = suspendCoroutine<Response<T>> { cont ->
enqueue(object: ApolloCall.Callback<T>() {
override fun onResponse(response: Response<T>) {
cont.resume(response)
}
override fun onFailure(e: ApolloException) {
cont.resumeWithException(e)
}
})
}
But the function in DataSource still has a onSuccess() and onError() as callbacks that needs to be passed to whoever call it.
fun saveStudents(
students: List<StudentInput>,
onSuccess: () -> Unit,
onError: (errorMessage: String) -> Unit) {
runBlocking {
try {
val response = GraphQLClient.apolloInstance
.mutate(CreateStudentsMutation
.builder()
.students(students)
.build())
.execute()
if (!response.hasErrors())
onSuccess()
else
onError("Response has errors!")
} catch (e: ApolloException) {
e.printStackTrace()
onError("Server error occurred!")
}
}
}
The SyncService class code changed to be like:
private fun runSync(onComplete: () -> Unit) = async(CommonPool) {
val syncStudentProcess = async(coroutineContext, start = CoroutineStart.LAZY) {
syncStudents()
}
val syncEnrollmentProcess = async(coroutineContext, start = CoroutineStart.LAZY) {
syncEnrollments()
}
syncStudentProcess.await()
syncEnrollmentProcess.await()
onComplete()
}
It does execute it sequentially, but I need a way to stop every other coroutine if any got any errors. Error that might come only from Apollo's
So I've been trying a lot to find a way to simplify this code, but didn't get any good result. I don't even know if this chaining of callbacks can be simplify at all. That's why I came here to see some thoughts on it.
TLDR: I want a way to execute all of my functions sequentially, and still be able to stop all coroutines if any got an exception without a lot o chaining callbacks.

Categories

Resources