AsyncTask accessing Database - Where to initialize, close?
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
精彩评论