如何在使用 ContentProvider 进行许多更改时暂停通知观察者
我有一个 ExpandableListView,它使用 SimpleCursorTreeAdapter,它使用 ContentProvider 返回的游标。这很好,因为它始终与数据保持同步,但有时我需要对数据库进行许多更改,以便在同一秒内多次重新查询游标。是否可以暂停ContentObservers的通知以避免不必要的重新查询?
I have an ExpandableListView that uses a SimpleCursorTreeAdapter which uses the cursors returned by a ContentProvider. This is fine as it always keeps in sync with the data but sometimes I need to do many changes to the database so that the cursor is requeried many times in the same second. Is it possible to suspend the notification of ContentObservers to avoid unnecessary requerys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能的解决方案是修改内容提供程序以允许暂停通知。要通知的 URI 将添加到队列中,直到禁用暂停为止。
一个特殊的 URI 来打开/关闭暂停(例如 content:///suspension):
我不确定如何最好地启用/禁用暂停,但一种可能是在 update() 方法中使用 对数据库进行更改的操作现在可以在 ContentProvider 启动时挂起它,并在其完成时禁用挂起。
A possible solution is to modify the content provider to allow suspending notifications. URIs to be notified are added to a queue until suspension is disabled.
I'm not sure how to best enable/disable suspension but one possibility would be to have a special URI which turns on/off suspension (e.g. content://<authority>/suspension) in the update() method:
The service that does the changes to the database can now suspend the ContentProvider when it starts and disable suspension when it finishes.
您可以使用 http://developer.android. com/reference/android/widget/BaseAdapter.html#unregisterDataSetObserver(android.database.DataSetObserver) 取消注册您的侦听器,并在您的工作准备就绪后再次注册它们,这对我来说听起来像是 AsyncTask 。
Can you use http://developer.android.com/reference/android/widget/BaseAdapter.html#unregisterDataSetObserver(android.database.DataSetObserver) to unRegister your listeners and once your work is ready, register them again? Sounds like a AsyncTask to me.