I am currently using RxJava on Android with Kotlin, but I have a problem and I cannot solve it without using toBlocking ().
I have a method in an employee service that returns an Observable>:
fun all(): Observable<List<Employee>>
That's all good, as this Observable emits a new list of employees whenever an employee changes. But I would like to create a PDF file from employees, which obviously should not be launched every time an employee changes. Also, I would like to return an object Completablefrom my PDF generator method. I want to add a title to my PDF file, and then iterate over the employees and calculate the salary of each employee who also returns Observable, and this is the place I'm using toBlocking right now. My current approach is this:
private fun generatePdf(outputStream: OutputStream): Completable {
return employeeService.all().map { employees ->
try {
addHeaderToPDF()
for (i in employees) {
val calculated = employeeService.calculateWage(i.id).toBlocking().first()
}
addFooterToPDF()
return @map Completable.complete()
}
catch (e: Exception) {
return @map Completable.error(e)
}
}.first().toCompletable()
Is there a way to make this code a little cleaner using RxJava?
Thanks in advance!
source
share