如何正确使用CALLER_IS_SYNCADAPTER
不知怎的,我不理解查询参数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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CALLER_IS_SYNCADAPTER 不一定影响数据库行中存储的内容,它取决于执行的命令。它不应该对查询产生影响。请勿从设备上的用户应用程序使用它。
现在...它为什么存在?
提供它是为了帮助notifyChange() / ContentObservers / ContentResolver / 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.
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 performedA 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.
您可以添加到现有的 Uri:
You can add to your existing Uri:
这是来自 Javadoc 的内容:
.
Here is from Javadoc:
.