android - Worker queue using RxJava -


i want create worker queue using rxjava: have single thread doing work, , want guarantee no other job executed until have finished/failed current job.

my solution block observable , wait result:

fun foo() : observable<foo> {     return observable.unsafecreate { subscriber ->         handlerthread.post {             val answer = object.performsomejob(whatever)                     .flatmap { object.performanotherjob(whatever) }                     .flatmap { object.performlastjob(whatever) }                     .blockingfirst()             subscriber.onnext(answer)             subscriber.oncomplete()         }     } } 

you may argue there no need use rxjava since everything's synchronous. that's true particular method, but:

  1. i want avoid 'callback hell': there 3 methods, each of taking callback , use rxjava chain them
  2. i use rx further on in caller method.

i know blocking considered anti-pattern, can better in case?

you can use concat perform work sequentially on thread:

fun foo(): observable<foo> {     return performsomejob(whatever)         .concatmap { performanotherjob(whatever) }         .concatmap { performlastjob(whatever) }         .subscribeon(schedulers.newthread()) } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -