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

1

30

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.

Gallicize answered 2/2, 2012 at 12:53 Comment(10)
Do you have a link to the Google IO video you reference?Inoffensive
I think that you can find all your answers in this project : github.com/necronet/Eli-G it has been detailed in this SO post.Gena
Hello Zakaria, I found that example a week ago but its a very dirty implementation of that pattern. It creates another Contentprovider to handle the REST instead of a service. Thanks for your reply though but im looking for the full implementation of the pattern as described in the image. @John youtube.com/watch?v=xHXn3Kg2IQEGallicize
@Zakaria, my previous reply was not correct. I confused this project with other code i had here. I already looked at Eli-g's code but im having a hard time grasping its logic. Im willing to approve an awnser which fully explains eli-g's logic. Thanks for the replies so farGallicize
@Samvdb : Did you have a look at the SO post where Eli-G explains that logic ?Gena
@Zakaria, yes i did but its not clear. What i understand is he followed pattern A from the google video. This would be acceptable for me to get a better understanding of how things work. So ill implement this method instead of option B. However Im not sure what the hell he is doing. In homeActivity he is starting up the LoaderService and TwitterService. Why 2 services? It seems like the LoaderService is all i need in order to get REST working though. Could you elaborate maybe further? ThanksGallicize
@Sam, hi there, I was just wondering how things worked out for you. Did you manage to implement pattern B exactly as proposed? I'm facing the same challenge right now, and it seems there still are no examples available - the only ones available being for option A (for my application's purpose, pattern B sounds more appropriate though).Imperceptible
@vaiomike, things worked out great, i spent alot of time trying to get the hang of it but once you got it. You will use it for all your projects! Unfortunatly i cant give you an example since the project i was working on is closed source (it doesnt belong to me anymore). Good luck with your implementation, i hope you succeed! Its worth it. On a side note: I studied the google IO 2011 app alot to look at their implementation and went on from there. They didnt fully use it like the scheme above.Gallicize
@Sam: too bad it's closed source - not a chance for sharing certain key components I suppose? either way - how closely did u match pattern B from above? quite exactly, or some noticeable variations? (for whatever reason, all examples/posts on the internet seem to handle pattern B only). thx!Imperceptible
@Imperceptible Source has become available: github.com/samvdb/TracknTraceGallicize
A
20

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://mcmap.net/q/257910/-need-sample-android-rest-client-project-which-implements-virgil-dobjanschi-rest-implementation-pattern: 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.

Adeleadelheid answered 8/2, 2012 at 15:19 Comment(1)
Datadroid claims to be an implementation of option A, not option B. Is it mistaken?Sportswoman

© 2022 - 2024 — McMap. All rights reserved.