0

I'm creating an android app where I store some objectives in an sqlite database. Also I'm fetching that content from the online mysql database if there's new content.

So when the main activity launches it loads the stored data from the sqlite db in a list view en then does a JsonObjectRequest with Volley. (An Async Task)

But still I'm getting the following errors when launching the main activity

Skipped 88 frames!  The application may be doing too much work on its main thread.
Skipped 34 frames!  The application may be doing too much work on its main thread.

The 88 frames is from the sqlite database, loading all the stored data. Because when I swipe down to refresh I only do the JsonObjectRequest en get the error with like 34 frames.

Do I maybe have to put the sqlite db loading also in an AsyncTask? Or do you have another solution?

I'd love to hear you opinion. Thanks in advance.

Wiljan

EDIT:

I now put it in an Async like this:

private class syncData extends AsyncTask<Void, Void, Void> {
    private KerntaakListAdapter adapter;

    public syncData(KerntaakListAdapter adapter) {
        this.adapter = adapter;
    }

    /**
     * The system calls this to perform work in a worker thread and
     * delivers it the parameters given to AsyncTask.execute()
     */
    @Override
    protected Void doInBackground(Void... params) {
        dataSource.syncData(swipeRefreshLayout);
        return null;
    }

    /**
     * The system calls this to perform work in the UI thread and delivers
     * the result from doInBackground()
     */
    protected void onPostExecute(String result) {
        adapter.notifyDataSetChanged();
    }
}

And trigger it with this:

new syncData(adapter).execute();

But it still gives me an skipped frames error. Although the frames are a bit reduced..

  • 1
    So you should try to read your database in a separate thread too; but there could be other things in your app causing frame drops – Eenvincible Jul 14 '16 at 20:34

1 Answers1

1

Do I maybe have to put the sqlite db loading also in an AsyncTask? Or do you have another solution?

Yes, by default all database operations are performed on the UI thread which will lead to skipped frames.

You should perform your queries on a background thread. Use anything that works best for you, it can be AsyncTask or even a java Thread.

I would recommend using a Loader framework for select queries because loader manager has a very convenient lifecycle attached to activity. They also allow you to monitor the underlying data and update it once it gets changed via ContentObservers.

If you use a ContentProvider you can also take a look at AsyncQueryHandler if you need other queries such as insert, update or delete.

Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Thnx for the tip! I tried it but it won't help yet.. Please read my edit in my question for further information. – Wiljan van Dalen Jul 14 '16 at 21:33
  • Sorry but the code you showed looks absolutely fine. If you still have skipped frames that means the problem is somewhere else.. Keep looking where you might be doing too much work on the UI thread. Maybe some calculations that can be moved to background, loding data, maybe the view hierarchy is inefficient and can be improved.. Take a look at http://stackoverflow.com/questions/14678593/the-application-may-be-doing-too-much-work-on-its-main-thread and keep looking.. Good luck :) – Gennadii Saprykin Jul 14 '16 at 21:37
  • You can update/post a new question if you find the root cause and still don't know how to fix it. So far it looks good. – Gennadii Saprykin Jul 14 '16 at 21:39
  • Thnx for your feedback. I have one big database class with all kinds of functions for reading and creating from the db, etc. So do I now have to put every function from that class in a AsyncTask? :o – Wiljan van Dalen Jul 15 '16 at 11:44
  • It depends on your application needs. Usually it's better to use loaders for select queries and threads for insert, update & delete. AsyncTask will work too. But before doing this, I would measure the performance first, just to make sure that you really need this improvement. Background threading will give you better performance, but it might be not noticeable if your database queries are fast enough. Check this link first: http://stackoverflow.com/questions/5047378/when-should-i-do-certain-sqlite-operations-on-another-threadnot-the-main-thread – Gennadii Saprykin Jul 15 '16 at 14:24
  • Do u maybe have a good example of a Loader framework of Loader example? And could u explain/give examples when an AsyncTask gets used most common and when a normal thread? – Wiljan van Dalen Jul 15 '16 at 15:26
  • I think you didn't get it :) I mean loaders from Android SDK: https://developer.android.com/guide/components/loaders.html – Gennadii Saprykin Jul 15 '16 at 15:51