Google IO Rest design pattern, finished ContentProvider and stuck on getting the data from the network

Sam picture Sam · Feb 2, 2012 · Viewed 10.6k times · Source

After watching the very known video on this topic I decided to go with design pattern B. Using a contentprovider with servicehelper.

google design pattern on rest

Basically I have the following files:

  • MyProvider
  • MyDatabase
  • Mycontract

In the activity I can now get the contentresolver and query the provider. All is working great so far.

Now I need to sync my contentprovider to fetch data from my REST API. Thus I need to implement a service helper service and Rest method. Studying the Google IO app has helped me a lot, I'm a novice with Android so it's still hard to figure it out.

I see Google uses RemoteHandlers to process the external data, I guess they are the Processor classes in the diagram?

What I don't understand is how I can implement the servicehelper + service part to get the data from the network.

  • Where do I call on the service helper?
  • What do the service and helper need to do exactly?
  • Are there any good examples of this exact design pattern?

I have read several topics on stack about this, all suggesting different methods. I found an example which declares a restprovider and then myProvider has to extend that provider. I don't like those solutions and want to follow this structured design pattern.

Answer

ChrLipp picture ChrLipp · Feb 8, 2012

In my understanding the pattern is:

  • Don't show an empty activity and load the content in the background. When the loading fails you cannot display anything.
  • Instead display data stored in the db accessible via a content provider and an adapter - this guarantees that the user always see a content
  • In the background fetch new data, once the data is on the phone the activity is automatically updated through the adapter

To your questions (I changed the order):

Where do i call on the service helper?
I choose pattern A from Vigils talk. In that case the call depends on your application. You could trigger the update when the application starts, when the activity is created or when the user selects an update button. I would choose at activity creation.

You have choosen pattern B. In that case it is clear that the content provider has to trigger the update. When? For getting new data: at creation time or after the first read access. I would use the creation time. For create, update, delete after the corresponding action in your content provider.

Are there any good examples of this exact design pattern?
From my post at https://stackoverflow.com/a/8693919/734687: the only open source reference implementation I know is available under http://datadroid.foxykeep.com. It is a library which you can use in your own application. The architecture is explained under /presentation - make sure you read it.

what does the service helper need to do exactly?
If you look at the slides at slide 19 it is a singleton which encapsulates the call to the service and handles the asynchronous calls via request ids.

what does the service need to do exactly?
The service (slide 17 in the presentation) just ensures that the action is performed in the background.