2

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?

James Black
  • 41,583
  • 10
  • 86
  • 166
  • How did you solve this? I have exactly the same situation and currently don't know how to start the service (prefer using a service for REST) from the content provider. – User Jul 24 '13 at 17:00
  • Ah... this could be more informative: http://stackoverflow.com/questions/9112658/google-io-rest-design-pattern-finished-contentprovider-and-stuck-now – User Jul 24 '13 at 17:01
  • @Ixx - I have shelved that project at the moment, so I haven't solved it yet, but I expect to go back to it by the end of the calendar year. – James Black Jul 25 '13 at 16:10

2 Answers2

1

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.

pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
0

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

Zheko
  • 673
  • 6
  • 16