0

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);




    }
}
Ingrid Cooper
  • 1,191
  • 3
  • 16
  • 27
  • it would be wiser to use async task here – karan Oct 15 '15 at 08:52
  • @Karan Mer I used AsyncTask too but I'm not sure how to convert IList to Java.Lang.Object. I have edited my question and posted my code. – Ingrid Cooper Oct 15 '15 at 09:18
  • first check this out, you are making it more complicated. http://stackoverflow.com/questions/14250989/how-to-use-asynctask-correctly-android – karan Oct 15 '15 at 09:22
  • @KaranMer But I need to return the IList once the background process is completed. Also, there is no Void type in Xamarin Android. – Ingrid Cooper Oct 15 '15 at 09:31
  • you can return list by specifying return type in the asynctask just change the 3rd argument in class defination to `IList` you can then return your list from doinbackground to onpostexecute – karan Oct 15 '15 at 09:34
  • @Karan Mer I have tried that too but it gives me this error: 'LoadingTask.DoInBackground(params Java.Lang.Object[])': return type must be 'Java.Lang.Object' to match overridden member 'Android.OS.AsyncTask.DoInBackground(params Java.Lang.Object[])' – Ingrid Cooper Oct 15 '15 at 09:40
  • can you post the changed code, – karan Oct 15 '15 at 09:42
  • @Kara Mer Sure. I have just posted my code with after returning IList – Ingrid Cooper Oct 15 '15 at 10:24
  • change your following 2 methods as below http://pastebin.com/94CjSzxb – karan Oct 15 '15 at 10:30
  • @KaranMer I tried that but after running it crashes with this exception: System.InvalidCastException: Cannot cast from source type to destination type. at return (Java.Lang.Object)dList in the DoInBackground() method – Ingrid Cooper Oct 15 '15 at 10:49
  • try the changed code http://pastebin.com/s88Dv6fk – karan Oct 15 '15 at 11:09
  • @KaranMer I tried that too but same crash ie; InvalidCastException – Ingrid Cooper Oct 15 '15 at 11:29

1 Answers1

0

Probably you are using a parser that loads the entire data and creates a set of objects to access that data (providing which is known as an abstraction layer). You should try to load the data in chunks, maybe this blog post can help you

lordscales91
  • 423
  • 1
  • 8
  • 22