I am a little new to android development. I know that network activity and expensive operations should not be done on the ui 开发者_JS百科thread, but what about database activity?
I have an activity that, when started, I query an sqlite database and dynamically populate rows in a tablelayout with an inflator. I do this all in the oncreate() method. Should this be done in a separate thread? If so, can anyone provide me with an example?
Thanks!
Depending on the database and the device, querying a database on the UI thread could possibly trigger an ANR message.
Android provides an easy way to perform asynchronous queries so you don't end up block the UI thread if it takes longer than expected. Check out the AsyncQueryHandler
in the docs and the IOSched has an implementation you can steal and use.
I wouldn't expect a local query to really slow the UI down much. Do you notice any hang when you first start the app? Things like fetching URLs over the network should be in their own thread because they can cause long hangs.
精彩评论