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

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -