5

I'm working on application whose main responsibility is to contact remote server and display the data provided.

Service is Soap based. For that I use ksoap library, but let's cut to the case.

I've been "calling service" with the use of asynchronous tasks. Everything seemed to go well, but...

Service is sequential, and tends to 'lose' my requests, so I don't get proper results.

So I decided to take a synchronous approach to resolve the issue, but this way I have to provide additional loading buttons/bars etc.

The performance is terrible in this way. What is the best way to handle such case ? What kind of synchronisation can I use so there won't be any race between the requests ?

How can I make use of Android Services ? How are those better?

Thank you in advance for answers.

Olek
  • 165
  • 1
  • 3
  • 10

1 Answers1

15

You can actually call the AsyncTask in a sync way:

class MyTask extends AsyncTask<Void,Void,String>
{...}

MyTask x = new MyTask();
String result = x.execute().get();

See the docs page for AsyncTask

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
  • 1
    Thank for the quick response. Have you tried it yourself ? So telling it to get() i guess it simply calls join() on the thread it executes on. Won't the get() suspend the UI or other tasks ? Are there any built in mechanisms to control the Tasks queue ? – Olek Feb 09 '12 at 20:50
  • I think I mostly replied to the "sync" part. I understood that you wanted to block the UI until you have your response back. And at least with onPreExecute and onPostExecute you can safely start progress bars and so on. – Heiko Rupp Feb 09 '12 at 20:55
  • Actually I don't want to freeze the UI. And I'm looking for a way to avoid this. I'm looking for a way of synchronization of most of the tasks. – Olek Feb 09 '12 at 21:10
  • You could in `doInBackground` run the individual there in order until you are satisfied with the result, then return. – Heiko Rupp Feb 09 '12 at 21:15
  • That seems reasonable. Thank you. I'll try this approach tommorow and see how it goes. – Olek Feb 09 '12 at 21:24