I'm writing a Service, a Content Provider, and multiple apps. The Service writes new data to the Content Provider's SQLite database every 5 minutes or so plus at user input, and is intended to run pretty much forever in the background. The app, when runnin开发者_运维技巧g, will display data pulled from the Content Provider, and will be refreshed whenever the Service puts more data into the Content Provider's database.
Given that the Service only inserts into the database once every five minutes, when is the right time to call SQLiteOpenHelper's getWritableDatabase()
/ getReadableDatabase()
? Is it on the onCreate()
of the Content Provider, or should I run it every time there is an insert()
and close it at the end of insert()
? The data being inserted every 5 minutes will contain multiple inserts.
With respect to your specific question, if you are going to use a ContentProvider
this way, you may wish to go the open-and-close-every-insert pattern. There is no onDestroy()
hook in ContentProvider
, so there's no great time to close the database. With more frequent/random access patterns, you just have to live with it. In your case, you may as well close it up.
That being said, I'm not clear why you're bothering with a ContentProvider
in the first place.
精彩评论