dispake
dispake

Reputation: 3329

Should all data be exposed via ContentProviders?

So I come mostly from a web background and I'm trying to learn the architecture of android apps. Trying to grasp a proper understanding of content providers.

What I think I'm understanding is that content providers are pretty much your middle man between the activity/services and your data (DAO of sort). I also think that content provider are also to provide other applications access to your data (almost like a web service?)

What I'm not really getting is, what if you did not need this data exposed to other applications? Do I need to bother with content providers and all the uri defining etc? If not, is there a name for this ... pattern? (or lack of) Or am I better off just using ContentProviders and just accept the added benefit of exposing this data to other apps?

I know eventually I will want to sync data between the app and a external database. I saw a google IO presentation supporting the pattern of using a content provider for RESTful communication. But for the time being, I'm just trying to get comfortable with basic static data. Then hopefully swap it out to data from a REST service down the line once I get it.

Hope I'm not completely off here. Thanks.

Upvotes: 1

Views: 369

Answers (2)

dten
dten

Reputation: 2374

Although it says that content providers are nice for sharing content they also give you a couple of other things free!

The best thing about content providers is that the system handles the threading issues for you :) when I ported my app over to Honeycomb i got lots of database errors (mind you ones that couldn't previously exist in gingerbread) I wasn't handling the access on different threads properly.

I quickly put a content provider on top of my database and well.. never looked back in that regards, it seems much easier once you've setup your content provider the code throughout the rest of the app is much nicer and you don't have to worry about concurrency

Upvotes: 1

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72331

As written in the specs, and as you mentioned :

Content providers store and retrieve data and make it accessible to all applications. They're the only way to share data across applications; there's no common storage area that all Android packages can access.

So if the data you are storing should not be accessible by other applications there is no need to bother with ContentProviders. As you can see on Android Data Storage there are several ways in which you could store your data.

My opinion is that in your case, for usage of REST Services, and data that is not to be shared with other application you should use SQLiteDatabase. You can find a good example SQLiteDatabase example here.

Upvotes: 1

Related Questions