When should one use RxJava Observable and when simple Callback on Android?

后端 未结 9 2204
庸人自扰
庸人自扰 2020-11-29 14:05

I\'m working on networking for my app. So I decided to try out Square\'s Retrofit. I see that they support simple Callback

@GET("/user/{id}/         


        
9条回答
  •  广开言路
    2020-11-29 14:51

    When you are creating an app for fun, a pet project, or a POC or the 1st prototype you use simple core android/java classes like callbacks, async tasks, loopers, threads, etc. They are simple to use and do not require any 3rd party lib integration. A big library integration just to build a small-time unchangeable project is illogical when something similar can be done right of the bat.

    However, these are like a very sharp knife. Using these in a production environment is always cool but also they'll have consequences. It's difficult to write safe concurrent code if you are not well versed with Clean coding and SOLID principles. You'll have to maintain a proper architecture to facilitate future changes and improve team productivity.

    On the other hand, concurrency libraries like RxJava, Co-routines etc are tried and tested over a billion times to help write production-ready concurrent code. Now again, it's not that using these libraries you are not writing concurrent code or abstracting away all the concurrency logic away. You still are. But now, it's visible and enforces a clear pattern for writing concurrent code throughout the codebase and more importantly throughout your development team.

    This is a major benefit of using a concurrency framework instead of the plain old core classes that deal with raw concurrency. However, don't misunderstand me. I'm a big believer in limiting the dependencies of the external library but in this specific case, you'll have to build a custom framework for your code-base which is a time-consuming task and can be done only after advance experience. Hence, concurrency frameworks are preferred over using plain classes like callbacks, etc.


    TL'DR

    If you are already using RxJava for concurrent coding throughout the code base, simply use RxJava Observable/Flowable. A wise question to ask then is should I use Observables to Flowables. If not then, go ahead and use a callable.

提交回复
热议问题