机器人 - CursorLoader和放大器; SQLite的没有内容提供商放大器、机器人、提供商、内容

2023-09-12 10:44:57 作者:我没你想的那么坚强

我知道这已经讨论过但我想询问事情的当前状态。我一定要创建一个ContentProvider的使用CursorLoader在SQLite数据库连接?

I know this has been discussed yet I wanted to ask about the current state of the matter. Do i have to create a ContentProvider to use CursorLoader in connection with a sqlite database?

我发现

Usage CursorLoader没有的ContentProvider

看起来正是我所期待的却是Emmby评论

Looks exactly what I was hoping for yet as Emmby commented

在用户应该知道的一个限制,这是它没有任何机制来刷新数据的变化(如装载机所应该做的)

所以另一种解决方案中提到

So another solution is mentioned

https://github.com/commonsguy/cwac-loaderex

再次一些缺点,指出

然而,要利用自动再查询的,则需要使用相同的加载器为UI以及用于更新,限制了其可用性后台服务。

当然,使用我们想要得到一切被引入其中的好处LoaderManager时。所以我的问题是,如果有一种方法可以使用​​LoaderManager在SQLite数据库连接,而无需实现内容提供商尚未有它的所有好处。

Of course when using LoaderManager we want to get all the benefits for which it was introduced. So my question is if there is a way to use LoaderManager in connection with a sqlite database without having to implement a content provider yet have all the benefits of it.

感谢

推荐答案

你提到在您的文章无论是两种实现提供所有的 CursorLoader 的好处除了接收通知的基本内容发生变化时,的能力。

The two implementations you mention in your post both offer all of the benefits of the CursorLoader except the ability to receive notifications when the underlying content changes.

我一直在寻找到这家最近了很多,我可以自信地告诉你,Android的API目前不提供这样的手段只有一个原始的 SQLiteDatabase (它仅提供了 ContentResolver的#有NotifyChange()光标#setNotificationUri()的方法,这是用来通知所有光标是S一定通知注册乌里)。

I've been looking into this a lot recently and I can confidently tell you that the Android API currently does not provide a means of doing this with only a raw SQLiteDatabase (it only provides the ContentResolver#notifyChange() and Cursor#setNotificationUri() methods, which are used to notify all Cursors registered under a certain notification Uri).

这是说,你的选择,现在是:

That said, your options right now are to:

实施观察者自己,能够从 SQLiteDatabase 当内容发生变化,并且在某种程度上能够传递这些通​​知到所有现有装载机在你的应用程序。我写了一个pretty的广泛 博客文章 如何实施装载机 s表示可能会派上用场,如果你想接受这项挑战。还是......

Implement an observer yourself that is capable of receiving notifications from the SQLiteDatabase when the content changes, and is somehow able to relay these notifications to all existing Loaders in your application. I wrote a pretty extensive blog post on how to implement Loaders that might come in handy if you wish to take on this challenge. Or...

使用马克·墨菲的 LoaderEx 库,只让使用的AsyncTask 操作他的图书馆提供了对数据库的修改。需要注意的是,为什么他的任务刷新的原因装载机是因为他们叫 onContentChanged 装载机后立即插入/更新/删除进行,有效地告诉装载机的内容发生了变化,它应该刷新其数据。

Use Mark Murphy's LoaderEx library and only make database modifications using the AsyncTask operations his library provides. Note that the reason why his tasks refresh the Loader is because they call onContentChanged on the Loader immediately after the insertion/update/delete is performed, effectively telling the Loader that the content has changed and that it should refresh its data.

只需使用的ContentProvider CursorLoader 键,您可以使用 ContentResolver的#有NotifyChange()方法来通知 CursorLoader 已发生变化的内容。

Just use a ContentProvider with a CursorLoader and you can use the ContentResolver#notifyChange() method to notify the CursorLoader that a content change has occurred.

我试图找出一个更好的解决方案,我会在未来报到,如果我能找到/实现一个,但现在这些都必须做的。

I'm trying to figure out a better solution, and I'll report back in the future if I ever find/implement one, but for now these will have to do.