ContentObserver 通知的顺序

发布于 2024-10-02 10:18:26 字数 653 浏览 2 评论 0原文

我正在查询 ContentProvider,循环遍历光标中的行,为每行创建一个对象并将该对象添加到列表中。我将每个对象注册为其自己 uri 的 ContentObserver。这工作正常,当更新和/或删除对象的数据库行时,对象会收到通知。我的问题是,如果数据库行被删除,我如何知道从列表中删除它对应的对象?

在对象的 onChange() 方法中,我正在查询 ContentProvider 的 _id,如果没有返回结果,我将设置一个“已删除”标志。我还将列表(实际上是列表周围的 ContentObserver 包装器)注册为主 ContentProvider uri 的 ContentObserver,并且在该 onChange() 方法中,我循环遍历列表并检查列表中每个对象的“已删除”标志列出并从列表中删除它,如果deleted==true。

然而,似乎列表是在各个对象之前得到通知的……因此,当我循环遍历列表时,相关对象的“已删除”标志尚未设置。

有什么方法可以控制 ContentObservers 收到更改通知的顺序吗?由于文档没有说什么,我怀疑答案是“否”,但我想我会问。

目前的情况还不错,因为下次列表收到通知时,它会删除之前删除的对象,因此,我列表中最多有 1 个额外的对象......但必须有一个“已批准”处理这个问题的方法,但由于我是 Android 开发的新手,我无法弄清楚它会是什么。有什么建议吗?

I'm querying a ContentProvider, looping thru the rows in the cursor, creating an object for each row and adding that object to a list. I'm registering each object as a ContentObserver of its own uri. That's working fine, the objects are getting notified when their DB row is updated and/or deleted. My problem is that if the DB row is deleted how I know to remove it's corresponding object from the list?

In the object's onChange() method, I'm querying the ContentProvider for it's _id and if I get no results back, I'm setting a "deleted" flag. I've also registered the list (actually a ContentObserver wrapper around the list) as a ContentObserver for the main ContentProvider uri, and in that onChange() method, I loop thru the list and check the "deleted" flag of each object in the list and remove it from the list if deleted==true.

However, it seems that the list is getting notified BEFORE the individual objects are...hence, as I loop thru the list the relevant object's "deleted" flag is not yet set.

Is there any way to control the order in which ContentObservers get notified of changes? since the docs don't say anything, I suspect the answer is "no", but thought I'd ask.

The current situation isn't that bad, since the next time the list gets a notification, it will remove the previously deleted object, hence, I have at most 1 extra object in the list...but there must be an "approved" way of handling this, but as I'm new to Android development I haven't been able to figure out what it would be. Any suggestions?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文