我正在开发一个具有以下情况的程序。
您想要查找配方,因此 Activity
将调用数据库 ContentProvider
。
该配方未存储在本地,因此它将调用 Web 服务来获取数据。
这些数据将存储在数据库中,因为我假设如果您不想保留本地副本,您会选择稍后删除它,但您可能想购物和做饭而不需要经常上网。
所以我认为我的设计可能变得过于复杂。
我目前有一个将调用 REST 服务的 Service
和一个用于访问数据库的 ContentProvider
。
我现在正在考虑用 ContentProvider
替换 Service
,因为我不需要长期运行的服务,因为它应该很少出现。
因此,活动将调用数据库 ContentProvider,如果查询为空,则 ContentProvider 将调用 REST ContentProvider,因为活动不应该关心数据来自哪里,然后数据库 ContentProvider 将在返回之前存储信息到活动。
这是适合我的场景的最佳方法,还是将 ContentProvider 链接在一起是一种不好的形式?
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?
发布评论
评论(2)
我认为这是非常合理的。但是,我认为您仍然可以保留该服务,但始终通过 ContentProvider 公开数据。这里的一个小问题是,您必须在 ContentProvider 中启动(或绑定)服务,并且在使用 ProviderTestcase2 测试 Provider 时会遇到问题,因为 MockContext 不支持启动服务。
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.这似乎是一个好方法。目前我正在开发类似的东西,我发现了这篇很棒的文章,作者一步一步地解释了一切,说明什么东西用于什么,什么是最好的方法等等。如果您在实施某些事情时遇到困难,请看一下:
http://programming-android.labs.oreilly.com/ch11.html
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