Callbacks for network analyst tasks

410
6
06-07-2013 06:03 AM
SusanMathew
New Contributor
Hi ,

I am new to Esri android developer scene. I have prior experience with the  Arcgis sdk for javascript and silverlight. I was trying to get  a basic routing application running on my device. For other Arcgis  APIs,  a callback method is provided by the API to handle the results of a network operation(like routing or query task). But in the Arcgis api for andoid, we are required to write Async tasks or handler methods. Is there a particular reason the android API was designed this way?. Why didnt ESRI make use of listeners/callbacks to handle the results from network operations. Writing our own handler methods not only takes a lot of time , but also makes it very difficult to write clean and reusable code.

Also, the routing sample makes use of handlers/threads while the query task sample makes use of the Asynctask class. Which is the better method to implement routing task?

Thanks
0 Kudos
6 Replies
AndyGup
Esri Regular Contributor
Hi, I have a few pointers that will hopefully shed some light on your comments and concerns.

>>Is there a particular reason the android API was designed this way?. Why didnt ESRI make use of listeners/callbacks to handle the results from network operations.

Please note, this is a native Android Operating System programming pattern that is adopted from its Java-based roots. Our API team tries their best to promote best practices for the Android/Java development environment. We take similar approaches for the other APIs you mentioned. For example, you'll see a different pattern for the iOS developer environment.

>>Which is the better method to implement routing task?

AsyncTask, Handlers and Threads all have their pluses and minuses and they are also related. The AsyncTask pattern is considered to be the easiest way to learn implementing multi-threading and to properly resynchronize results back to the main user interface thread. Since Android is multi-threaded it is recommended to understand all three of these approaches when building applications. There are some excellent discussions on StackOverflow for example: http://stackoverflow.com/questions/6964011/handler-vs-asynctask-vs-thread.

For reference, here is an Android Best Practices article on Processes and Threads.

These concepts, patterns and practices may seem a bit foreign at first if you've been using other programming languages. I hope these links help get you pointed in the right direction.

-Andy
0 Kudos
ThomasBinu
New Contributor III
Here is an excellent post on creating a resuable Asynctask class
http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui

You can follow the same approach for the NA tasks in Esri and implement the callback in your main class.
0 Kudos
adityamehta
New Contributor
How do I implement a timeout for a  querytask running on a Asynctask class.
0 Kudos
ThomasBinu
New Contributor III
Dear Andy,
        Although I understand that using Asynctask classes and threads is the native android way, since NA tasks like routing, querytasks are always supposed to run outside the UI thread, couldnt this threading be build into the API itself. Similar to how libraries like volley(new android library for networking) or this http library is designed ,  the  API can provide listeners and callbacks for handling the results of a NA operation?.

Thanks,
Tom
0 Kudos
AndyGup
Esri Regular Contributor
Aditya, whoops, missed your question. Hopefully my answer isn't too late. You want to check out AsyncTask.get() http://developer.android.com/reference/android/os/AsyncTask.html#get%28long,%20java.util.concurrent.....

-Andy
0 Kudos
AndyGup
Esri Regular Contributor
Thomas, good point, thanks. Yes, I agree that for some common use cases that pattern could significantly simplify things. I recommend you either submit an enhancement request at http://support.esri.com/en/, and/or post it to http://ideas.arcgis.com/. Both of those are monitored for good ideas, and they provide a mechanism for officially tracking them.

-Andy
0 Kudos