Android ContentProvider vs ORMLite
Asked Answered
N

4

9

I'm using content provider to manage my application data, which is not shared with other applications. However, there is some boilerplate code such as translating the cursor to domain object. ORMLite provides a more elegant way that mapping the database entry to domain object directly. So I want to replace the content provider with ORMlite. But I have some worry:

  1. The content provider works fine in the multiple threads environment. I don't know whether the ORMlite is thread-safe.
  2. The content provider can broadcast the database change. I don't know whether the ORMLite has this kind of support.
  3. Android provider some utilities such as AsynQueryHandler, CursorLoader to do the asynchronusly query the database. If I use ORMLite, I have to explicitly create a thread or AsyncTask to do the query.

whether I should replace the content provider with ORMLite ?

Nitza answered 13/9, 2013 at 5:44 Comment(0)
C
15

I do not think, you can compare with an ORM with Content Provider. ORMLite offers following features link to user and has complete separate goal when developed.

But beside the things you mention, their are couple of other benefits of content provider.

  1. if you want to share date between multiple application or process. May be right now you do not have any plan to do so, but seems like you app is heavy on database, and in future if you plan to share your data with third party or your another app you have an option with content provider.

  2. Content provider is standard throughout all release of Android, that also means that your app is safe with that. I am pretty sure ORMLite is stable too. But do you really wanna take that risk, specially if that not saving your time or you do not have any business requirement to do so. Android API is already fragmented do you want to take another work load on top of that?

  3. if you wanna upload data to cloud it is easy to integrate with SyncAdapter

  4. Use android security and permission feature.

You are right about the extra code needed to process Content Provider.

Right now i am working on a project we have around 20 tables. I have created a DAO for every tables. Which basically internally uses Content provider.

DAO->Content Provider->SQL Lite Open Helper

Class XyzDao{
    private final Context mContext;
    XyzDao(Context context){
        this.mContext=context;
    }
    public String getMyData(){
        //content provider code
        return myData;
    }
    public void setMyData(String x, int y, double z){
        //content provider code to set the data
    }
}

I could have done without Content Provider and right now with current requirement that would worked fine but still i choose this route. You will ask probably why?

  • Asynchronous Operation
  • Easy Integration with the platform/cloud.
  • Less code less bug so less drama (think if i have to implement those tasks we just discussed)

And, the best part is from any Activity i can simply use my DAO to access database.

Alternatively you can directly access SQLLiteOpenHelper from your DAO.

At the end of the day every choice depend on your business requirement. If you have lots of persistence objects ORMLite could be a good choice.

Colvin answered 13/9, 2013 at 6:52 Comment(0)
S
1

On one side I prefer make ER model using OrmLite, on the other side ContentProvider provides some benefits as described above. That's why I have implemented universal ContentProvider which allows to have access to OrmLite ER model. Here it is https://github.com/blandware/android-atleap

Shemeka answered 8/1, 2014 at 16:42 Comment(0)
T
0

Library https://github.com/jakenjarvis/Android-OrmLiteContentProvider/blob/master/README.md provide solution for you task.

Thirteenth answered 11/8, 2014 at 15:38 Comment(0)
B
0

I created an ORM library that you can use that will basically combine both use cases for you, it is called CPOrm and will do what you require. You can use it to manage the creation and querying of your database, and everything is done through android content providers. The performance is also kind of good, with queries returning 9000+ results per second.

Brynne answered 18/4, 2015 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.