Asynchronous processes and how we deal with it
In our previous blog post we told you that we are using Retrofit and we discussed the pros and cons of our decision. Today’s topic will be asynchronous processes and how we handle them in our Android app. When your are developing an pp which communicates with an API, you will always have to deal with asynchronous processes. The client sends a request to the API and the API sends a response to the client. The problem is that the API does not respond instantly, which means that the client has to wait for the APIs response before being able to process it In the beginning this was a big problem, because the client is not supposed to halt while waiting for the API to send the requested data. If it did, the app would be stuck with an ANR every timeit is waiting for data from the API.To solve this problem we, started to use Otto, an event bus designed to decouple different parts of the application while still allowing those parts to communicate efficiently.The App could now push all kinds of events onto the event bus and process them asynchronously.
Let’s look at that in detail: Let’s assume the App has to send data to the API and wants to know if the API received the data and returned a response. To solve this using our event bus, the pp needs three methods:
@Subscribe public void sendMessage(SendMessageEvent event) { //Send the message to the API } @Subscribe public void messageSendSucessfull(MessageSendSuccessfulEvent event) { //Successful } @Subscribe public void messageSendFailed(MessageSendSuccessfulEvent event) { /Error handling to let the user know what’s wrong }
The name of the methods are actually not important, what matters are the @Subscribe annotations and the arguments to the methods, which describe which event types the method subscribes to. Of course the methods do not have to reside in the same class but each class they reside in needs to register with the event bus. To push an even to the event bus, we only have to have a reference to our initialized event bus and call it’s `post` method with an event object.
this.eventBus.post(new SendMesageEvent(“Hello World”));
The snippet above is a short example of how to send a message to an API endpoint. For example think about a typical chat client like whatsapp. When you send a message to a participant the app puts your message into the list of messages before it got a response of the API. When the app gets a response of the API it puts a checkmark behind your text or in case of an error it puts an error behind your text and you have to resend it. In the code snippet with the different event methods you can see two response types, a successful response and a failed response. In both methods, you can implement what the app should do when everything is fine or handle the error to inform the user what went wrong. This is a good solution to handle asynchronous processes and to deal with errors as well. Of course there are other libraries to solve this problem, but we are content with using Otto as an event bus and that is what matters. If your are using a different solution or you’ve built a similar architecture to our’s, feel free to shoot us an email, we are always interested to see what others are doing!












