如何处理REST调用,数据持久化,同步和观察的ContentProvider [英] How to handle REST calls, data persistence, syncing and observing ContentProvider

查看:334
本文介绍了如何处理REST调用,数据持久化,同步和观察的ContentProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过很多次,但我觉得我努力的目标的问题是有一点不同,也许更复杂。

我要开发应用的 REST风格的Web服务并需要具备以下条件的申请:

  • 应用程序应该表现出一些书,它们的作者和他们的编辑列表和详细

  • 该应用程序还应该允许搜索一本书

  • 书籍,作者和编辑们从RESTful Web服务获取

  • 每一个实体都被缓存,这样,当我打开一个活动我第一次看到旧的数据(如果有的话),而来自网络的新的更新。

  • 每一个实体更新时间,有关各方应通知( ContentObserver ?一个普通的监听器实施?)

  • 如果一个呼叫已在执行(说 API /书籍/ 1337 API /编辑)主叫方应通知其加载数据,应给予旧的(如果存在的话),就好像它是原来的调用者。

  • 一些数据(仅适用于书籍和作者)应更新每隔N分钟(由用户决定)和观察员应通知(<$ C C $> SyncAdapter ?)

问题的:

观看和学习都在提出维吉尔Dobjanschi组件后谷歌I / O 2010这里是我的疑惑:

  1. 我怎么能透明地处理了实体是-更新的理念会因任何调用?我应该使用 ContentObserver 的ContentProvider 我将必须实现?

  2. 如果我使用的 ContentObserver 我可以轻松地设置一个状态标志的单个实体(由Dobjanschi的建议),例如:更新,强行嵌入,等等。但是我应该怎么处理列表?说我想要一个书单,在那里我应该把状态标志?我应该把它放在只列出了一个状态表?如果是这样,我的观察的两个光标 S,一个是状态和一个用于实际列表(即表/内容URI)。而如果我问的实体不存在(还)或REST调用返回 404 ?如何处理回调?

  3. 如果我把我所有的REST方法在 ** SyncAdapter ** ,我可以强制 SyncAdapter 更新从网络实体/实体列表(并因此将其放入合适的表)?这样一来,状态标志将是有益的。

  4. 可以在 SyncAdapter 多个实体工作(实际上,实体名单,因为我想更新图书和编辑每一个现在,话),因为它只有一个 performSync 的方法?

  5. 如果我的 SyncAdapter 的实施已经在设备设置被禁用的用户将不更新任何内容(这很好)。但是,如果用户在活动中点击更新书籍按钮,可我还是叫了 performSync 的方法,还是会被停用?

解决方案

SyncAdapter是一种设计模式包括五个部分:

  1. 的应用程序。它使用了一组活动连同光标 ContentObserver 也许的CursorAdapter 有的提供从的ContentProvider
  2. 系统UI到本地存储的数据
  3. 的ContentProvider 本地设备的数据存储。处理CRUD通话,手柄通知 SyncAdapter 的需要更新推送到服务器。
  4. 帐户在远程服务器上的用户身份。
  5. SyncAdapter A中运行,并保持本地数据存储,同步与服务器后台进程。
  6. 在服务器本身。

所以。要问题:

  1. 是,更新的意思,有尚未被推到服务器。这是你连续在数据库中设置一个标志。它在ContentProvider的设置,当你创建/更新本地更改/删除。这一行SyncAdapter运行时,它看到的标志,更新推到服务器,清除标志的标志本身做了两件事:
    告诉用户该应用程序是忙保存更改,并在这样做了。
    标记行的更改,因此SyncAdapter知道把它推到服务器。
    <一href="http://stackoverflow.com/questions/6588770/$p$pvent-network-sync-trigger-when-syncing-from-network-in-android-contentprovide/6602629#6602629">Read这里了解更多详情。

  2. 如果你不同步的整个目录,那么你的客户会直接查询服务器,并把它们放入ContentProvider的缓存结果。没有状态标志那里,因为他们是来自服务器,因此匹配服务器的状态。写下您的SyncAdapter忽略它们,或者放弃他们,他们已经缓存在几天之后。

  3. 要确保本地的更新会发送到服务器,你写你的ContentProvider中的ContentProvider的创建/更新时通知SyncAdapter /删除通话。 <一href="http://stackoverflow.com/questions/6588770/$p$pvent-network-sync-trigger-when-syncing-from-network-in-android-contentprovide/6602629#6602629">(Read在这里...)
    以确保您定期从服务器获取更新,则配置帐户进行自动同步。 <一href="http://stackoverflow.com/questions/5253858/why-does-contentresolver-requestsync-not-trigger-a-sync/5255360#5255360">(Read在这里...)

  4. 是的。 performSync仅仅是一个函数调用。写它做你想要的。拥有它取表1从服务器,并把它变成一个表在你的ContentProvider。然后把它取表2,并把它变成一个不同的表。等等。

  5. 您可以通过调用强制同步 ContentResolver.RequestSync() ContentResolver.SYNC_EXTRAS_MANUAL 的额外软件包。
    您可以手动获取一些与客户端code,并直接推入的ContentProvider。

I know that this question has been asked too many times, but I think the issues I'm trying to target are a little bit different, maybe more complicated.

I am going to develop an application that uses a RESTful Web Service and needs to have the following requirements:

  • the app should show some books, their authors and their editors in lists and in detail

  • the app should also allow searching for a book

  • books, authors and editors are fetched from a RESTful web service

  • every entity has to be cached so that when I open an Activity I see the old data first (if any), while the new one updates from the network.

  • every time an entity is updating, the interested parties should be notified (ContentObserver? A regular Listener implementation?)

  • if a call is already executing (say to api/books/1337 or to api/editors) the caller should be notified that it is loading data and should be given the old one (if it exists), as if it was the original caller.

  • some data (only books and authors) should be updated every N minutes (decided by the user) and the observers should be notified (SyncAdapter?)

Questions:

After watching and studying all of the components proposed by Virgil Dobjanschi at Google I/O 2010 here are my doubts:

  1. How can I transparently handle the "entity-is-updating" concept for any caller? Should I use ContentObserver on a ContentProvider I will have to implement?

  2. If I use a ContentObserver I can easily set a status-flag for the single entity (as suggested by Dobjanschi), for example UPDATING, INSERTING, and so on. But how should I handle list? Say I want a list of books, where should I put the status flag? Should I put it in a status table for lists only? If so, I should observe two Cursors, one for the status and one for the actual list (i.e., the table/Content URI). And what if the entity I'm asking for does not exists (yet) or the REST call returns a 404? How do I handle the callback?

  3. If I put all of my REST methods in a **SyncAdapter**, can I "force" the SyncAdapter to update an entity/entity list from the network (and therefore put it into the proper table)? This way, the status flag would be useful.

  4. Can the SyncAdapter work on multiple entities (actually, entity lists, as I want to update books and editors every now and then), since it only has a performSync method?

  5. If my SyncAdapter implementation has been disabled by the user in the device settings it won't update anything (and that's fine). But if the user clicks on an "update books" button in an Activity, can I still call the performSync method, or will it be disabled as well?

解决方案

SyncAdapter is a design pattern involving five components:

  1. An app. This uses a set of Activity along with Cursor and ContentObserver and maybe CursorAdapter and some to provide a UI to the locally stored data from a ContentProvider.
  2. ContentProvider The local device's data-store. Handles CRUD calls, handles notifying SyncAdapter of the need to push an update to the server.
  3. Account The user identity on the remote server.
  4. SyncAdapter A background process which runs, and keeps the local datastore in sync with the server.
  5. The server itself.

So. To the questions:

  1. "Is-updating" means, "has local changes which have not yet been pushed to the server. It's a flag you set on a row in your database. It's set in ContentProvider when you Create/Update/Delete a row. When SyncAdapter runs, it sees the flag, pushes the update to the server, clears the flag. The flag itself does two things:
    a. Tells the user the app is busy saving the change, and when that's done.
    b. Marks the row as changed so SyncAdapter knows to push it to the server.
    Read here for more details.

  2. If you're not syncing the entire catalog, then your client will directly query the server and cache the results by putting them into the ContentProvider. There is no status flag there, since they're coming from the server and therefore match the server state. Write your SyncAdapter to ignore them, or perhaps discard them after they've been cached a few days.

  3. a. To ensure your local updates get sent to the server, you write your ContentProvider to notify the SyncAdapter during the ContentProvider's Create/Update/Delete calls. (Read here...)
    b. To ensure you get updates from the server periodically, you configure the account for automatic sync. (Read Here...)

  4. Yes. performSync is just a function call. Write it to do what you want. Have it fetch table 1 from the server and put it into one table in your ContentProvider. Then have it fetch table 2, and put it into a different table. Etc.

  5. a. You can force a sync by calling ContentResolver.RequestSync() with ContentResolver.SYNC_EXTRAS_MANUAL in the extras bundle.
    b. You can manually fetch something with client code and directly push it into the ContentProvider.

这篇关于如何处理REST调用,数据持久化,同步和观察的ContentProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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