P
P
pyatigil2014-06-05 16:02:29
Android
pyatigil, 2014-06-05 16:02:29

Android callback hell: how to get rid of spaghetti code?

I was surprised to find that promise libraries that are popular in the JS world are practically not used in android (I managed to find only jdeferred, which does not seem to be very popular).
On the other hand, APIs are usually designed to accept custom callbacks instead of working with standard Futures, so you can't use the standard libraries (util.concurrent) either.
How does the community solve this problem?
PS code example: apparently due to the limitations of multithreading and interprocess communication, libraries (from the standard android SDK and / or Google Play etc) often suffer from the fact that you need to build long chains of callbacks that pass parameters to each other. Something like that:

lib.connect(new OnConnectCallback() {
void run {
  lib.login(new OnLoginCallback() {
    void run {
      lib.stream(new OnStreamCallback() {
         <...>
      }
    }
  }
}
})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pyatigil, 2014-07-02
@pyatigil

I think I found: habrahabr.ru/company/2gis/blog/228125
RxJava or Bolts-Android is exactly what I was missing

O
one pavel, 2014-06-05
@onepavel

I used to do this too, and also annoyed by the mass of implementations when declaring the class
class Derived implements Callback1, Callback2 ...
methods were mixed, delimitation by comments did not help much.
I started creating variables with anonymous implementation of
OnConnectCallback interfaces mOnConnectCallbacknew = OnConnectCallback() {
void run {
}
};
the rest of the strapping is done by closures

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question