Reputation: 524
Is it ok to create ContentProvider for each table means multiple content provider in a single app ?
I want to do that for various reasons.
1- As my app grows the single ContentProvider class become bloated and messy.
2- I don't want to share whole ContentProvider class with another app. I just want to share few or only one table.
Upvotes: 1
Views: 1866
Reputation: 3872
In general you can have multiple content providers in one app, so that's not a problem at all. Just make sure that both content providers have different authorities, otherwise it won't work. Content provider authorities have to be unique.
However, there are more things to consider when deciding whether to go with one or multiple content providers. Here are some of them:
Your question kind of implies that you plan to share the same SQLite database (I've figured that from the term "table") in both content providers, which is not a good idea. Every content provider would probably have its own instance of the SQLiteOpenHelper
for the database which means you may run into locking issues, see this answer: https://stackoverflow.com/a/3689883/1558654
You probably can find a way to share the same SQLiteOpenHelper
instance with both content providers, but that would most likely result in a bad architecture and a probably other issues in the long run.
So if you want to use multiple content providers, make sure both content providers have their own dedicated authorities and databases, which also means that you can't use certain database features like foreign key constraints (or you'll have to take care of that on your own) or transactions (if they would span both databases).
Regarding your reason #1: Not sure if you're talking about the interface (the contract) or about the actual .java file, but that doesn't really matter, since it's merely an architectural problem that's definitely solvable when having a single content provider. If your content provider class file is too large your architecture is probably not so good.
Also the decision whether you should use two content providers or not depends a lot on what kind of data they provide.
Is the data related (or is it about the same kind of data)? It's probably better to use just one content provider.
It's completely unrelated? Two content providers are probably the better choice.
Reason #2 needs more consideration. The question is why don't you want to expose certain tables? Is it a security / privacy issue? Or is it just that other apps won't need the data?
If it's for security or privacy, it's probably better to use two content providers and set android:exported="false"
on the one holding the sensitive data. But even with one content provider there are ways to protect these.
If security or privacy are not an issue, just don't publish the content URIs to the other tables in your public contract.
Upvotes: 6
Reputation: 11038
Yes, that is fine. As you point out it allows each one to contain specific pieces of data and each one can have the appropriate permissions.
Upvotes: 0