Todd Davies
Todd Davies

Reputation: 5522

Android: Should a database within an app be accessed in an asynchronous way?

I'm making an app that uses a SQLite database to store data. Sometimes my app freezes when switching between activities that access the database, especially when the activity being started accesses the database in the onCreate() method.

Should I:

Could you point me to relevant code samples if nessecary please?

Thanks in advance!

Upvotes: 2

Views: 1416

Answers (2)

Todd Davies
Todd Davies

Reputation: 5522

I have a better solution:

I am using a pipeline system, where I have runnables that are posted into the pipeline that get executed in the order they were posted. I find this solution easy to manage, neat and efficient.

An example of pipelining is here: http://mindtherobot.com/blog/159/android-guts-intro-to-loopers-and-handlers/

Upvotes: 3

IronBCC
IronBCC

Reputation: 429

Yes, yor must use asynch access, this is best practice. SDK contains AsynchTask especially for this.

BuyAsyncTask extends AsyncTask<Item, Void, Item> {
    @Override
    protected Item doInBackground(Item... params) {
        ..use DB here
    }

    @Override
    protected void onPreExecute() {
        ..some easy operation befor start thread
    }

    @Override
    protected void onPostExecute(Item resultTask) {
        ..do what you want, thread is finish
    }

}

Upvotes: 4

Related Questions