Reputation: 41858
I am working on a program that has the following situation.
You want to look up a recipe, so the Activity
will call the db ContentProvider
.
The recipe isn't stored locally, so it will call out to a web service to get the data.
This data will be stored in the database as I am assuming that if you don't want to keep a local copy you will choose to delete it later, but you may want to shop and cook without going to the Internet constantly.
So I think my design may be getting overly complicated.
I currently have a Service
that will call the REST service, and a ContentProvider
to go to the database.
I am now considering replacing the Service
with a ContentProvider
, as I don't need a long-running Service as it should infrequently go out.
So, the Activities would call the db ContentProvider, and if the query is empty then the ContentProvider would call the REST ContentProvider, as the Activity shouldn't care where the data comes from, and the db ContentProvider would then store the information before returning back to the Activity.
Is this the best approach for my scenario, or is it bad form to have ContentProviders chained together?
Upvotes: 2
Views: 987
Reputation: 40761
I think that is quite reasonable. However, I think you could still keep the Service but just always expose the data through the ContentProvider. One glitch here is that you will have to start(or bind) the service in the ContentProvider and you will have problems when testing your Provider using ProviderTestcase2<Provider>
as the MockContext does not support starting the service.
Upvotes: 1
Reputation: 703
It seems a good approach. Currently I'm developing something similar and I've found this great article, where the author explains everything step by step, saying which thing for what is used for, what is the best approach and so on. Take a look at it if you are having some troubles implementing something: http://programming-android.labs.oreilly.com/ch11.html
Upvotes: 0