从网络上的Andr​​oid的ContentProvider同步时prevent网络同步环网络、Andr、oid、ContentProvider

2023-09-12 08:34:06 作者:爱到忘记流月经ノ

我在写我自己的ContentProvider将使用SyncAdapter同步到Web服务。

I'm writing my own ContentProvider which will be synced to a web service using a SyncAdapter.

在同步适配器正在修改的内容提供商的数据提供商触发网络同步时,在内部调用getContentResolver()。有NotifyChange导致同步循环问题发生。

Problem happens when the sync adapter is modifying the content provider's data the provider triggers a network sync when internally calling getContentResolver().notifyChange causing a sync loop.

与网络同步标志的有NotifyChange需要,当客户端应用程序执行的变形,但是当同步适配器正在修改,应避免对

The notifyChange with the network sync flag is required for when a client application does the modification but should be avoided when the sync adapter is modifying.

如何之一,ContentProvider的里面,伊斯利如果它正在使用的客户端应用程序(它应该触发后修改网络同步)告诉或通过同步适配器(应该不会触发网络同步)。

How can one, inside a contentprovider, easly tell if it's being used by a client application (which should trigger network sync upon modification) or by a sync adapter (which should not trigger network sync).

目前我使用的是不同的CONTENT_URI的(同步适配器存取使用CONTENT_URI_NO_SYNC数据,并使用CONTENT_URI客户端应用程序),以便能够在两种类型的访问进行区分,并设置相应的网络同步标志。

Currently I'm using different CONTENT_URI's (sync adapter accesses the data using a CONTENT_URI_NO_SYNC and client apps using a CONTENT_URI) to be able to distinguish between the two types of access and set the network sync flag accordingly.

推荐答案

关注此视频关于REST API的使用在 SyncAdapter 秒。

他们讨论的方法,是将一组元数据标志的列添加到数据库中。这使我们能够做的三件事。

The method they discuss is to add a set of metadata flags columns to the database. This allows us to do 3 things.

该标志本身允许 SyncAdapter 来确定需要改变和该等改变的行。你怎么知道本地创建行和本地修改行之间的区别?此外,你怎么知道要进行哪些REST API调用?如果你只是的删除的行,请问你的 SyncAdapter 知道现在的数据已经一去不复返了该行被删​​除?相反,将应删除标志,然后,当 SyncAdapter 运行,它知道推删除服务器。

The flags themselves allow the SyncAdapter to determine the rows that need changes and what those changes are. How do you tell the difference between a locally created row and a locally modified row? Furthermore how do you know which REST API call to make? If you just delete a row, how does your SyncAdapter know the row to be deleted if the data is now gone? Instead, set the "Should be deleted" flag, and then, when the SyncAdapter runs, it knows to push a delete to the server.

中的标志让你的的CursorAdapter 修改所创建的视图(如添加一个微调来显示,该行正在同步)

The flags allow your CursorAdapter to modify the view that is created (like adding a Spinner to show that "This row is being synced")

最后,这一点,他们的没有的指出,这些标志让你知道为什么该行被修改。如果没有标志设置,行的变化,它的必须的已经因为来自服务器的更新。因此,无需同步到网络。

Finally, and this they don't point out, the flags allow you to tell why the row is being modified. If none of the flags are set and the row changes, it must have been because of an update from the server. Therefore, no need to sync to network.

因此​​,两个工作流如下:

So, the two workflows are as follows:

本地变化

在应用程序创建新行。行创造标志是真的。 ContentProvider的存储行,看到创建标志,所以它叫有NotifyChange(...,真); 同步到网络=真(最后一个参数)使 SyncAdapter 火灾。 SyncAdapter 扫描数据库,查找与创建标志设置的行,并执行相应的服务器操作。成功后, SyncAdapter 清除该标志。(行更新 ContentProvivder 的ContentProvider 看到标志清晰,无标志留一套,所以要求有NotifyChange(...,假); ContentObserver 看看国旗的变化,更新,看起来像同步完成 App creates new row. Row "create" flag is true. ContentProvider stores the row, sees create flag and so it calls notifyChange(...,true); Sync to network = true (the final parameter) causes SyncAdapter to fire. SyncAdapter scans the database, finds the row with create flag set and performs appropriate server action. After success, SyncAdapter clears the flag.(row update on ContentProvivder) ContentProvider sees the flag clear, no flags are left set, so it calls notifyChange(...,false); ContentObservers see the flag change, update to look like "sync finished"

所有这些步骤都相当于更新/删除 - 每个可同步行的一个标志,对每个创建/更新/删除的。 还要注意对方赢 - 如果创建暂时失败?关闭服务器......你怎么知道重试? - 简单,你不要清除创建标志,你在15分钟后看到它

All these steps are equivalent for update / delete -- one flag per syncable row for each of create/update/delete. Also notice the other win -- what if "Create" fails temporarily? server down... How do you know to retry? -- Simple, you don't clear the "Create" flag and you see it 15 minutes later.

远程修改

SyncAdapter 火灾是由于周期性同步。 SyncAdapter 获取来自服务器的更新。将更改到数据库中。不设置任何标志。 的ContentProvider 认为缺乏标志,知道改变必须有来自服务器(或不是,需要推到服务器的数据库的变化),所以它要求有NotifyChange(...,假); ContentObserver 看看内容的变化,所以他们与新行的数据更新 SyncAdapter fires due to periodic sync. SyncAdapter fetches an update from the server. Pushes changes into the database. Doesn't set any flags. ContentProvider sees the lack of flags, knows the change must have come from the server (or isn't a database change that needs to be pushed to the server), so it calls notifyChange(...,false); ContentObservers see the content change and so they update with new row data