开发者

Question about datàbase activity in android onCreate()c

开发者 https://www.devze.com 2023-01-25 16:47 出处:网络
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 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号