I am writing an application that consists of business logic and UI parts. There is quite big amount of data to be stored and accessed/modified by both BL and UI. In most of the cases changes to the stored data should be reflected by UI immediately.
How do I decide whether I should or should not use direct DB access? content provider?
I have done some reading on the subject (1, 2) and I understand the difference between these two options.
Please share your thoughts on other aspects of the problem, such as performance, difficulty level of code development and maintainability, etc.
A content URI is a URI that identifies data in a provider. Content URIs include the symbolic name of the entire provider (its authority) and a name that points to a table (a path).
A content provider can be used to manage access to a variety of data storage sources, including both structured data, such as a SQLite relational database, or unstructured data such as image files. For more information on the types of storage available on Android, see Storage options, as well as Designing data storage.
A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content using insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database.
Create a class in the same directory where the that MainActivity file resides and this class must extend the ContentProvider base class. To access the content, define a content provider URI address. Create a database to store the application data. Implement the six abstract methods of ContentProvider class.
In the apps that I've written, I've found that once you get past the learning curve, implementing a ContentProvider is pretty easy.
Pros:
Cons:
When I was trying to figure out how to implement a ContentProvider, I poured over the example code in Google's I/O application. Before you make a decision, I would at least spend a day prototyping one so you can get first-hand experience of the tradeoffs.
I would recommend spending that extra time and energy to write your ContentProvider. ContentProviders are not just about sharing access to data. The advantages
ContentObservers
notifyChange
All in all, a ContentProvider is a very beautiful Android concept which is well-worth implementing. And once you have your definitions in place, it is not very difficult to add support for more data. It will then be as easy as writing your database code in a Helper class or your business-logic classes.
** EDIT ** Here's a utility which generates content provider code from model classes: https://code.google.com/p/mdsd-android-content-provider/
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