Thursday, 11 June 2015

How email fails to share experience or help others..

- Help me Obi-Wan Kenobi, you're my only hope!
Projection light goes off and Obi-Wan turns to Luke...

Well we are all familiar with the scene right? Sending a holographic video message with an astro droid is pretty much like sending an email (it is electronic, send to a recipient can even have attachments :) ) and it saved the galaxy.

However, I don't think thats the case with real life. I started programming at an early age on Commodore 64, of course it was Basic. When I got my Amiga, I discovered a new more capable programming language called Amos. One day I managed to find a copy of an Amos manual. It was just a bad photocopy but come on it was much before internet and it was all I need to boost my coding skills on Amos. Very soon I became pretty popular between high school friends who are interested in Amos. I read code sniplets from the book on phone to help them solve their code issues.

Since than I enjoyed anyone who asked for help. Tried reply every single email, write blogs, give talks and even write a book (it is exhausting!!). However, a recent event just made me realize email is not the platform to share experience or help each other.

I was contacted by a kind lady whom was a friend of a friend seeking help. Basically she had an activity which fires a TimerTask to retrieve data from backend periodically. When the user navigates to another activity the dialog from the TimerTask continued to show up and actually that was it.

The email was actually not longer than a paragraph of text with the activity code.



Most of you may have already figured out what is wrong. The activity fires an AsyncTask but since the AsyncTask ran on its own thread, it has no idea if the activity is still in use, paused or even destroyed. However, there was more. The backend was a Secure SOAP Webservice. So each time the asynctask is executed, it creates a new connection, does login, prepares the soap message, makes request, decodes the soap response and finally displays the data which was actually only a two integer!

So I wrote a long detailed email to point each problem...


- Calling a soap webservice from a mobile device is waste of bandwidth. The backend service can be wrapped into a simple rest service which could serve the same 2 digit integer in either xml or json. This would also enable to encapsulate the changes in webservice.
- AsyncTask is easy and great but not for repetitive and long running jobs. Creating an asynctask is expensive. Besides creating asynctask periodically may also cause to follow the order of the responses. The proper way to retrieve data periodically from a backend would be either using Services or via ContentProvider (with or without a Loader). This way the connection can be created for only once and the login process doesn't need to take place each time. Also since the activity would either stop listening updates or unbinds the service so the data won't popup on other activities.
- Finally if all those create a huge burden (provided that this is just a school project, which need to be delivered tomorrow and won't go live ever) than Volley can be used instead of AsyncTask, which would cancel the request when the activity is not active as well as giving the option to cancel or pending requests.

Btw, my email started with asking if she would consider posting the question to StackOverFlow and if she does, I would love to re write the solution once again there.

..and she disappeared :) of course I am not expecting a box of chocolate or a thanks note but don't have any idea if she liked the answer or what level of fix she decided to implement. Besides all the data is lost forever, no one would benefit from my answer or may be her brilliant idea which overrides my solution.

So.. not really sure, email really helps and acts as a tool to solve problems or share knowledge at all..