Codevalley
Codevalley

Reputation: 4641

Android app hangs at SQL select query

My program involves interaction with SQLite in a fairly regular basis, and in the beginning of the app, I call a query

mDatabase.rawQuery("SELECT key, 
                           indice as _id
                      FROM Dictionary",null);

Strangely the app stalls on executing this line. This does not happen if I am debugging the application, but when I run the app, the control goes from this line and never returns. I have checked this by putting logcat before and after this line. I have not been able to comprehend this behavior. Can someone help?

P.S. The table Dictionary has over 2000-3000 records.

EDIT: I have tried calling this from both UI & separate threads. Either ways, the execution stops at this call (for that thread). So when I call it from another thread, though there is no ANR, the call still fails and holds the thread indefinitely.

EDIT2: This issue does not happen every time I run the application but 5 out of 10 times. And apparently happens more on weaker phones.

Upvotes: 0

Views: 823

Answers (3)

Kartik Domadiya
Kartik Domadiya

Reputation: 29968

Yes piyushnp and abhinav are right. AsyncTask or Thread are better option for getting the details. Show progressbar when doing background processing. And when you get query results display it in activity or do whatver processing you want on query results.

This example simulates your problem. AsyncTask basic Example : AsyncTask

Upvotes: 0

Piyush Patel
Piyush Patel

Reputation: 1825

Take care of below points.

  • Make call to query in separate thread other than UI thread.
  • Cursor at max can hold upto 1MB of data. So query for minimum amount of data.

Upvotes: 2

Abhinav
Abhinav

Reputation: 39894

You should take this off the UI thread. Looks like a heavy call. Anything which takes longer than 5 seconds and stalls the UI thread will trigger an ANR.

Upvotes: 1

Related Questions