Android: access SQLite database via Content Provider or implement DAO?
Asked Answered
E

3

17

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?

Esquivel answered 24/11, 2012 at 16:3 Comment(0)
D
13

I prefer to use ContentProvider if you have concerns of closing or locking of db. Check Simple Content Provider for db operations

Devoid answered 24/11, 2012 at 16:24 Comment(1)
i am facing database is locked problem in multi-process. Should i use content Provider to solve that problem?Afire
G
10

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?

Germanic answered 24/4, 2015 at 10:52 Comment(1)
After all reading I decided to go to the content provider option. Things got easy in further development, for database management and syncingGermanic
N
5

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

Nahamas answered 18/1, 2013 at 18:54 Comment(2)
This is not relevant anymore since ContentProvider (event internal) is the best data source for CursorLoader)Hereabouts
Keep in mind that up to Android 4.2 content provider are by default available to other Android applications. Only since 4.3 default is exported:falseDolorous

© 2022 - 2024 — McMap. All rights reserved.