Google IO Rest设计模式,完成ContentProvider并坚持从网络获取数据 [英] Google IO Rest design pattern, finished ContentProvider and stuck on getting the data from the network

查看:139
本文介绍了Google IO Rest设计模式,完成ContentProvider并坚持从网络获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

观看了有关该主题的著名视频后,我决定采用设计模式B.将contentprovider与servicehelper结合使用.

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

基本上我有以下文件:

  • MyProvider
  • MyDatabase
  • 我的合同

在活动中,我现在可以获取contentresolver并查询提供程序.到目前为止,一切都很好.

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

现在,我需要同步contentprovider以从REST API中获取数据. 因此,我需要实现服务助手服务和Rest方法. 研究Google IO应用程序对我有很大帮助,我是Android的新手,因此仍然很难弄清楚.

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.

我看到Google使用RemoteHandlers处理外部数据,我猜它们是图中的Processor类吗?

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

我不了解的是如何实现servicehelper + service部分以从网络获取数据.

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

  • 我在哪里致电服务助手?
  • 服务和帮手到底需要做什么?
  • 这种精确的设计模式有很好的例子吗?

我已经阅读了几本关于此的主题,都提出了不同的方法.我发现了一个声明了restprovider的示例,然后myProvider必须扩展该提供程序.我不喜欢这些解决方案,而是希望遵循这种结构化的设计模式.

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.

推荐答案

在我的理解中,模式是:

In my understanding the pattern is:

  • 不显示空白活动,而是在后台加载内容.加载失败时,您将无法显示任何内容.
  • 而是显示存储在db中的数据,可通过内容提供者和适配器访问该数据-这样可以确保用户始终看到内容
  • 在后台获取新数据时,一旦数据在电话中,活动就会通过适配器自动更新

对您的问题(我改变了顺序):

To your questions (I changed the order):

我在哪里致电服务助手?
我从Vigils talk中选择模式A.在这种情况下,调用取决于您的应用程序.您可以在应用程序启动,活动创建或用户选择更新按钮时触发更新.我将在活动创建时选择.

您已经选择了模式B.在这种情况下,很明显,内容提供者必须触发更新.什么时候?对于获取新数据:在创建时或在首次读取访问之后.我会用创建时间.对于创建,更新,删除,请在您的内容提供者中执行相应的操作.

是否有这种精确设计模式的好例子?
从我的帖子 https://stackoverflow.com/a/8693919/734687 :我知道的唯一开源参考实现可在 http://datadroid.foxykeep.com 下找到.它是一个库,您可以在自己的应用程序中使用.在/presentation下解释了该体系结构-请确保已阅读.

服务助手需要做什么?
如果您查看幻灯片的幻灯片 19是一个单例,它封装了对服务的调用并通过请求ID处理异步调用.

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.

该服务需要做什么?
该服务(演示文稿中的幻灯片17)仅确保该操作在后台执行.

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.

这篇关于Google IO Rest设计模式,完成ContentProvider并坚持从网络获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆