如何正确使用CALLER_IS_SYNCADAPTER

发布于 2024-11-28 04:09:15 字数 326 浏览 0 评论 0原文

不知怎的,我不理解查询参数CALLER_IS_SYNCADAPTER的工作概念。它的默认值为 false,如果设置,则不会自动设置 DIRTY 标志。那么它到底意味着什么呢?根据我的理解,联系人的每次更改都会导致将脏标志设置为 1。同步适配器完成作业后,使用带有 CALLER_IS_SYNCADAPTER 的插入/更新/删除,插入/更新和删除的记录应将脏标志设置为 0 ,是吗?

但是,如果我使用该可选参数调用查询,条目将保留标志 1。

我还需要做其他事情吗?或者我对它应该如何工作的理解是否错误?或者有什么东西可以告诉系统同步已成功完成以设置标志?

有人有样本或一些进一步阅读的建议吗?

somehow I don't understand the working concept of the query parameter CALLER_IS_SYNCADAPTER. Its default value is false, if set, the DIRTY flag is not automatically set. So what does it actually mean? Out from my understanding, each change on a contact results in setting the dirty flag to 1. After a sync adapter finished the job, using insert/update/delete with the CALLER_IS_SYNCADAPTER the inserted/updated and deleted records should have a dirty flag of 0, is that right?

However if I invoke queries with that optional parameter, the entries remain with the flag 1.

Is there something else I have to do, or is my understanding how it should work wrong? Or is there something to tell the system the sync has been finished successfully to set the flags?

Does anybody have a sample or some advices for further reading?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˇ宁静的妩媚 2024-12-05 04:09:15

CALLER_IS_SYNCADAPTER 不一定影响数据库行中存储的内容,它取决于执行的命令。它不应该对查询产生影响。请勿从设备上的用户应用程序使用它。

现在...它为什么存在?

提供它是为了帮助notifyChange() / ContentObservers / ContentResolver / Syncadapter 集成。更改数据库中的行有两种用例。

  1. 本地用户从应用程序进行编辑。
  2. 更改来自网络(通过 SyncAdapter)

任一更改都需要更新 UI(如果它在屏幕上)。因此 ContentResolver.notifyChange(Uri uri, ContentObserverobserver, booleansyncToNetwork) 被调用。这会更新 ContentObservers 并告诉它们从 ContentProvider DB 中获取最新数据。调用中的最后一个参数就是您的线索。

ContentResolver本身就是一个ContentObserver。当它看到数据库更改时,它会考虑启动 SyncAdapter 将更改推送到网络。这在情况 1 中非常有用。在情况 2 中,它是多余的。更改来自网络,根本没有理由启动同步来将更改发回。

Calendar.CALLER_IS_SYNCADAPTER 是 SyncAdapter 执行的 update() 中使用的提示。当它为 true 时,ContentProvider 将 syncToNetwork 设置为 false,确保不执行冗余的第二次同步

第二个示例如 veljko 提到的。从服务器删除事物的最简洁方法是设置删除标志,然后执行同步。当 CALLER_IS_SYNCADAPTER 标志为 false(用户应用程序)时,对 delete() 的调用会设置该标志。当该标志为 true(正在发生同步)时,对 delete() 的调用会将删除内容推送到服务器并从本地数据库中删除该行。只有一个delete() 调用,该标志允许ContentProvider 知道它应该执行哪一项任务。

CALLER_IS_SYNCADAPTER doesn't necessarily affect what's stored in the database row, it depends on the command performed. It shouldn't have an effect on queries. Do not use it from a user application on the device.

Now... Why does it exist?

It is provided to help with notifyChange() / ContentObservers / ContentResolver / Syncadapter integration. There are two use cases for changing a row in the database.

  1. Local user edits from an application.
  2. Changes come from the network (via SyncAdapter)

Either change requires the UI to update, if it's onscreen. Therefore ContentResolver.notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) gets called. This updates ContentObservers and tells them to go fetch the newest data from the ContentProvider DB. That last parameter in the call is your clue.

ContentResolver itself is a ContentObserver. When it sees the database change, it considers starting up your SyncAdapter to push the change up to the network. This is great in case 1. In case 2, it's redundant. The change came from the network, there's no reason at all to start up a sync to send the change back.

Calendar.CALLER_IS_SYNCADAPTER is a cue used within the update() performed by the SyncAdapter. When it's true, ContentProvider sets syncToNetwork as false, ensuring a redundant second sync is not performed

A second example is as veljko mentioned. The cleanest way to delete a thing from the server is to set the delete flag, and then perform a sync. When the CALLER_IS_SYNCADAPTER flag is false (user app) a call to delete() sets the flag. When the flag is true (sync is happening), a call to delete() pushes the deletion up to the server and removes the row from the local DB. There's only one delete() call, this flag allows the ContentProvider to know which task it's supposed to do.

桃扇骨 2024-12-05 04:09:15

您可以添加到现有的 Uri:

myUri=calendarUri.buildUpon().appendQueryParameter(Calendar.CALLER_IS_SYNCADAPTER, "true").build();

You can add to your existing Uri:

myUri=calendarUri.buildUpon().appendQueryParameter(Calendar.CALLER_IS_SYNCADAPTER, "true").build();
梦回梦里 2024-12-05 04:09:15

这是来自 Javadoc 的内容:

/**
* 可选的插入、更新或删除 URI 参数,允许调用者
* 指定它是同步适配器。默认值为 false。如果属实
* 脏标志不会自动设置并且“syncToNetwork”参数
* 调用时设置为 false
* {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}。
*/

.

调用resolver.delete(...)不会立即删除原始联系人行。相反,它会在原始联系人上设置 DELETED 标志,并从其聚合联系人中删除原始联系人。然后,同步适配器从服务器删除原始联系人,并通过再次调用resolver.delete(...)并传递CALLER_IS_SYNCADAPTER查询参数来完成电话端删除。

Here is from Javadoc:

/**
* An optional insert, update or delete URI parameter that allows the caller
* to specify that it is a sync adapter. The default value is false. If true
* the dirty flag is not automatically set and the "syncToNetwork" parameter
* is set to false when calling
* {@link ContentResolver#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)}.
*/

.

The invocation of resolver.delete(...), does not immediately delete a raw contacts row. Instead, it sets the DELETED flag on the raw contact and removes the raw contact from its aggregate contact. The sync adapter then deletes the raw contact from the server and finalizes phone-side deletion by calling resolver.delete(...) again and passing the CALLER_IS_SYNCADAPTER query parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文