In my app I send a request to the server and get a JSON response containing a lot of JSON objects (about 25,000). Then I try to parse it by deserializing it. When sending the request to the server I show a progress dialog and after the response is received I dismiss it. But the progress dialog gets stuck after the response is received because the deserializing takes time. So to avoid the UI getting stuck I'm showing a progress dialog before deserializing and put the deserializing code in a Thread and then in the RunOnUiThread() method I'm dismissing the progress dialog. It works only for the first time but from the second time onwards again my UI gets stuck. The below is my code:
mProgressDialog = new ProgressDialog (this);
mProgressDialog = ProgressDialog.Show (this, null, "Loading list", false, false);
new Thread(new ThreadStart(() =>
{
drugsList = Newtonsoft.Json.JsonConvert.DeserializeObject<IList<Drugs>>(resp1);
RunOnUiThread(() => DismissDialog ());
})).Start();
Could you please let me know if there is any way to resolve this issue?
Thank you.
Edit:
I have tried to use an Async Task
too but I'm not sure how to return IList<Drugs>
from the DoInBackground()
method. It saying that it can return only Java.Lang.Object
type. The below is my code:
public class LoadingTask : AsyncTask
{
private ProgressDialog _progressDialog;
private String _resp1;
private Context _context;
public LoadingTask (Context context, String resp1)
{
_context = context;
_resp1 = resp1;
}
protected override void OnPreExecute()
{
base.OnPreExecute();
_progressDialog = ProgressDialog.Show(_context, "Loading In Progress", "Please wait...");
}
protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
{
IList<Drugs> dList = JSONHelper.DeserializeToList<Drugs>(@params[0].ToString());
return dList.ToString();
}
protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);
_progressDialog.Dismiss();
loadDrugs ((IList<Drugs>)result);
}
}
I'm not sure how to return IList<Drugs>
instead of Java.Lang.Object
.
My Drugs
class extends Java.Lang.Object
ie; public class Drugs : Java.Lang.Object
Edit 2:
After returning IList<Drugs>
in DoInBackground()
:
public class LoadingTask : AsyncTask
{
private ProgressDialog _progressDialog;
private String _resp1;
private Context _context;
public LoadingTask (Context context, String resp1)
{
_context = context;
_resp1 = resp1;
}
protected override void OnPreExecute()
{
base.OnPreExecute();
_progressDialog = ProgressDialog.Show(_context, "Login In Progress", "Please wait...");
}
protected override IList<Drugs> DoInBackground(params Java.Lang.Object[] @params)
{
IList<Drugs> dList = JSONHelper.DeserializeToList<Drugs>(@params[0].ToString());
return dList;
}
protected override void OnPostExecute(IList<Drugs> result)
{
base.OnPostExecute(result);
_progressDialog.Dismiss();
loadDrugs ((IList<Drugs>)result);
}
}