I'm wondering which is the best approach to access my application database: use a Content Provider, or implement my DAO by hand? From my latest investigations, seems that Content Provider, even for app internal use, is preferable, but I don't know exactly what are the drawbacks of each approach. Can you give some feedback about this?
I prefer to use ContentProvider if you have concerns of closing or locking of db. Check Simple Content Provider for db operations
From Google Docs.
Before You Start Building
Before you start building a provider, do the following:Decide if you need a content provider.
You need to build a content provider if you want to provide one or more of the following features:
- You want to offer complex data or files to other applications.
- You want to allow users to copy complex data from your app into other apps.
- You want to provide custom search suggestions using the search framework.
You don't need a provider to use an SQLite database if the use is entirely within your own application.
But then I got a bit confused when reading this and some other posts. Does it make sense to use content provider event if it is meant to be used only by your own app?
From the ContentProvider documentation:
Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don't need to share data amongst multiple applications you can use a database directly via SQLiteDatabase.
Seems to me that, if you're not going to share data with other applications, you do not need a content provider.
Link: http://developer.android.com/reference/android/content/ContentProvider.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With