开发者

AsyncTask accessing Database - Where to initialize, close?

开发者 https://www.devze.com 2023-02-19 16:20 出处:网络
When accessing a database within an AsyncTask, should the database connection be a member of the parent Activity or opened/closed within each As开发者_运维技巧yncTask?

When accessing a database within an AsyncTask, should the database connection be a member of the parent Activity or opened/closed within each As开发者_运维技巧yncTask?

If the connection is a member of the activity, I'm concerned that when switching between my application's activities there is the possibility that the database connection could be closed before the AsyncTask utilizing it has completed.


Not something I've ever really thought about but instinctively I'd make the AsyncTask as self-contained as possible, i.e., have it handle its own db operations internally. The same applies to any threaded code.

EDIT: Here's a link to a good article discussing multiple access to SQLite in Android...Android SQLite Locking

0

精彩评论

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

关注公众号