-
Title:
Why Content Providers Matter - Developing Android Apps
-
Description:
-
Before we go ahead and build a content provider
-
you wouldn't be out of line to ask, well why.
-
The simplest answer is that it allows you to
-
share your data safely and efficiently across app boundaries by
-
abstracting the underlying data source, be it SQLite like
-
this or files or really anything else. So that other
-
apps can access it without really needing to understand how
-
you stored it. In fact, the calendar, SMS, and contacts
-
APIs work that way, using shared content providers.
-
We're going to share our weather database later, but if
-
you're not planning to expose your app's data, you're
-
probably thinking you can skip this bit, right? Well,
-
almost, in a lot of cases you could,
-
but really shouldn't. For example, in Sunshine we're using
-
SQ Lite. But you could be storing data in
-
files, dynamic run-time data or even just a different
-
database library. By using content providers, it's easier for
-
you to potentially switch out the data source and
-
much easier for someone other than you to manage
-
the UI layer code without them having to understand
-
the depths of your data storage implementation. On the
-
UI layer, it's a generic mechanism that returns cursors.
-
The same of those returned by SQLite databases. So,
-
if your data layer implementation changes, then your content provider
-
is effected. Still, it's just you writing the code right
-
now and that's a lot of boilerplate for the sake of
-
following a neat design pattern. Well, keep in mind that
-
as far as the framework is concerned, all data is handled
-
through content providers. So, if you want to interact with
-
anything outside of your app, such as sending data to a
-
widget or returning search results from the newer app, you'll need
-
a content provider for that too. In fact, that's how the
-
Google play store and Gmail widgets work. As well as the
-
ability to get search results from Google Play. Similarly, there's a bunch
-
of APIs designed to optimize the process of synching and querying data,
-
and updating UI accordingly. And all of them also expect content providers.
-
That includes sync adapters and cursor loaders. Which make your
-
app able to efficiently sync with your server, load data in
-
your UI layer, and which include built in content observers that
-
will update your UI automatically when the underlying data changes. You
-
could, of course, build all of that yourself but at
-
a certain point the advantage you gained by not writing a
-
content provider to begin with is lost in the process of
-
having to recreate all of the useful clusters that utilize it.
-
We'll take a look at publishing your content provider and using Lotus
-
to access it efficiently later in this lesson. And we'll explore sync adapters
-
in lesson six when we look at doing efficient background updates. But
-
first, Dan is going to show you how to actually build a content provider.