Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ContentProvider vs ORMLite

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 ?

like image 473
manshuai Avatar asked Sep 13 '13 05:09

manshuai


Video Answer


1 Answers

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.

like image 118
minhaz Avatar answered Sep 20 '22 09:09

minhaz