Issue
In my Android application I have several Activities that all connect to one service. This service establishes a connection to a server and handles the sending and retrieving of data (xml files in most cases). Inside of my Activities I call the service functions inside of AsyncTask, because I need to do following actions based on the answer of the server. Here is an example of the structure:
String xmlString = "<myXmlString><myDataObject></myDataObject></myXmlString>";
final AsyncTask<Void, Void, Boolean> myTask = new AsyncTask<Void, Void, Boolean>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog("Send Data...");
}
@Override
protected Boolean doInBackground(Void... params) {
int rc = mService.callMethod("MYFUNCTION", xmlString);
if (rc == 0) {
return true;
}
else {
return false;
}
}
@Override
protected void onPostExecute(Boolean success) {
hideProgressDialog();
if (success) {
// handle received data
// here it also might occur that another AsyncTask is called
// e.g.: final AsyncTask<Void, Void, Boolean> nestedTask = new AsyncTask<Void, Void, Boolean>() { ... }
} else {
// handle error
}
}
};
myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Handler handler = new Handler();
Runnable taskCanceler = new Runnable() {
@Override
public void run() {
if (myTask.getStatus() == AsyncTask.Status.RUNNING) {
myTask.cancel(true);
hideProgressDialog();
}
}
};
handler.postDelayed(taskCanceler, 15000);
Since I updated Android Studio it says that "This AsyncTask should be static or leaks might occur". Furthermore the nested AsyncTasks can be quite confusing sometimes, but they depend on each other so I don't really know how to change those tasks to static classes. Should I use a different approach for my needs or can I transform them into static classes and still keep the nested logic alive?
I hope I could make my issues clear. Thanks in advance!
Solution
Asyntasks were introduced as a background method of synchronization with the ability to post results to main thread when they finish. However, they have big limitations when the logic is complicated.
They are hard to cancel properly, if they are not used correctly they can leak memory and they are cumbersome to use when there are several operations to run.
Finally, they have a hard limit on the number of them that you can execute at the same time.
Here, you will find a list of libraries that you can use as a replacement of Asynctask
If you are a more experienced programmer then I suggest you go one step ahead and use one of the following technologies:
RxJava: This has mainstream in android development for the last couple of years. It will help you to handle business logic, and API calls in a reactive way. The learning curve is quite steep at the beginning but later will make your life easier. Here's an intro tutorial
Kotlin Coroutines: If you're into kotlin coroutines present an easy way to handle concurrency. Here you can find some tutorials
Answered By - Imanol
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.