views:

207

answers:

1

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 running, 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.

+1  A: 

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.

CommonsWare
From my understanding, it's how you can share data across apps - I'm planning to write multiple apps that use the same data. (I suppose another way could be to attach the data to the service and have each app query the service, but I'm not sure what the advantages and disadvantages of each option will be.)
jawonlee
@jawonlee: Ah, sorry, you didn't mention the multiple-apps part in your question.
CommonsWare