I would like to run asynchronous server call using AsyncTaskLoader. Method loadInBackground() should return the result synchronously. I am returning null and I call deliverResult() in callback which is executed on manually created HandlerThread. This implementation is sending to UI null at first, then (when async server operation is done) real data. I'm not sure this is good solution.
How to implement custom AsyncTaskLoader properly, where I want to load data by asynchronous server calls (thus using callbacks)?
Thanks for any help
use join() on manually created HandlerThread (without code it's hard to give a working example)
Related
I'm trying to implement a call screening service in my app. It seems the CallScreeningService class has really a bad design however. The abstract method onScreenCall is called on UI thread and it's not possible to use something different, in addition the method respondToCall must be called in onScreenCall because it's not possible to go async looking at AOSP source code. The code calls recycle() on onScreenCall arguments when it returns. The question: how is it supposed to work? We can't bind another service, we can't use an AsyncTask, even a load from database would be a problem since the access is performed on UI thread. Am I missing anything?
You can use rxjava or coroutines to access your data in background thread
I have really searched for this every where, I can make both synchronous and asynchronous data requests, but I can't actually understand which is asynchronous with what? and what is sync with what?
call.execute() runs the request on the current thread.
call.enqueue(callback) runs the request on a background thread, and runs the callback on the current thread.
You generally don't want to run call.execute() on the main thread because it'll crash, but you also don't want to run call.enqueue() on a background thread.
when you asynchronous, it means not in the foreground(it does not block the users interface while it accomplishes the given task), on other hand synchronous means in the foreground while your application execute things in the same thread the UI consuming.
In your case(making REST requests via retrofit or any other REST api) you must not make that in that foreground and you have to make in a background thread.
In the case of retrofit you have the following methods to make the request:
call.execute() // works in the foreground.
call.enqueue() // works in the background.
So you have a choice of two: either you make the call.enqueue directly or you can user call.execute but wrapped with a service(I mean you have to handle the background work your self).
Synchronous requests are declared by defining a return type.Synchronous methods are executed on the main thread. That means the UI blocks during request execution and no interaction is possible for this period. Using the .execute() method on a call object will perform the synchronous request. The deserialized response body is available via the .body() method on the response object.
Asynchronous requests don’t have a return type. Instead, the defined method requires a typed callback as last method parameter.Using asynchronous requests forces you to implement a Callback with its two callback methods: success and failure. When calling the asynchronous getTasks() method from a service class, you have to implement a new Callback and define what should be done once the request finishes.
Retrofit is a type-safe HTTP client for Android and Java. And I would highly recommend using this over any other library.
Do you understand what is sync and async call, or what is blocking and non-blocking call?
To answer your question, any api call you do or any heavy or time-consuming task you do on Android, it should be non-blocking (async) as it should not block the Main or UI thread in Android.
Please read this article for more understanding
https://developer.android.com/guide/components/processes-and-threads.html
I'm using Volley for all my network related code. Since Volley does the actual networking part off the main UI thread I havent really thought much about it but I realize now that in a few instances I do some significant processing of the response data in onResponse(). It seems like this code does indeed run on the main thread. What's the best way to do this? Should I define a AsyncTask for that specific portion of the code or should I just put the entire network request in a AsyncTask despite Volley's threading mechanisms.
I would suggest NOT to use AsyncTask as it has its own drawbacks in case activity is destroyed or recreated. And moreover if you are using Volley's queueing mechanism, this will just work as a add-on.
One solution I can think of is to use Loaders. Make the network call and process the response in loadInBackground method. And when onLoadFinished is called, you'll get the processed response.
Another solution (Not recommended and not so efficient) is to use a Service. You can make the network call from the Service and process the data and communicate back to the Activity.
I have the next simple question. I have an AsyncTask that connects my client to a server. For each task the server does I need another one AsyncTask class?
Let's say that I need a task to fill an array from the server, another to return an integer from the server and one to return an object from the server to the client, I need 3 AsyncTask classes, one for each of these tasks?
AsyncTask is good when you don't need to present any information to your user. Something like saving data to the server, that nobody really cares when it finishes.
When you need something to be loaded for the screen, you'd be better off with AsyncTaskLoader class, that provides all the necessary callbacks.
Besides that, I'd create as many AsyncTasks as necessary. They are cheap, they make the work done. Why worry?
Here is what you can do
1) you can call your 1 st async task from class A and add an interface to that asynctask which has one method like onfirstTaskCompleted()
2) call onfirstTaskCompleted() method in onPostExecute of your asyncTask
3) in class A implement onfirstTaskCompleted() and start an new Async task from there.
and repeat the same steps to 2nd and also third async task
You need AsyncTask whenever you want to do an operation asynchronously.
You do the asynchronous part in the doInBackground() of your AsyncTask.
If you can do everything inside a single doInBackground then you need just one AsyncTask.
Such as connect, get data, disconnect.
If you must do 2 separate asynchronous operations then 2 AsyncTasks. Such as connect, get data in one task. Then present them to the user. Then send request to server to delete some data, say, selected the data selected by the user, this you do in another AsyncTask. Or you can use the same asynctask somehow have a different flow of execution in doInBackground for different scenarios.
For my Android app I need to call a web page from activity, and wait until a response is received from remote server. So I think I need to use a new thread. Can I use AsynkTask? How can I tell to my activity that it must wait for respose to AsynkTask process?
check asyncTask
And you can use callbacks to inform the activity that the task has finished
check https://stackoverflow.com/a/13947857/1434631
Yes, you can use AsyncTask
AsyncTask has a method called onPostExecute() which lets you know when your background process, in this case, loading data from server, is completed.
Here is a nice tutorial !