服务中的数据库调用

发布于 2024-09-16 13:23:21 字数 724 浏览 7 评论 0原文

我一直在观看 Virgil 在 Google I/O 大会上关于 REST 密集型应用程序的演讲。

Google I/O 2010 - Android REST 客户端应用程序

尽管记事本教程直接进行数据库调用从 UI 层来看,Virgil 建议在服务中进行数据库调用。

目前,我的 Activity 的 onCreate 方法使用扩展的 ContentProvider 来访问 SQLite 数据库,以查询要附加到 Activity 的 ListView 的 Cursor。

我想将此代码移至服务中。很容易。我的问题是,将 Cursor 传输回 UI 层的适当方法是什么?我看到提出了很多问题,似乎总是有人建议有更合适的方法来做到这一点。那是什么方式?

更具体地说,到目前为止,我认为活动应该注册为某种侦听器。当在 Service 中检索 Cursor 时,会向 UI 层设置一个通知来访问它。服务将其推到哪里以便活动可以抓住它?

另外,我考虑的架构是有一个扩展服务,由活动调用。在该服务中,数据库事务将通过使用扩展的 ContentProvider 进行,任何侦听活动都将收到通知,并且将启动线程来访问 Web 服务。因此,我有 1 个扩展的 Service、1 个扩展的 ContentProvider 和几个用于不同 Web 服务的扩展线程。这看起来可以接受吗?

I've been watching Virgil's presentation at the Google I/O on REST-heavy applications.

Google I/O 2010 - Android REST client applications

Though the notepad tutorial makes database calls directly from the UI layer, Virgil suggests making database calls in a Service.

At the moment, my Activity's onCreate method uses an extended ContentProvider to access an SQLite database to query a Cursor to attach to the Activity's ListView.

I'd like to move this code into a Service. Easy enough. My question is, what is the appropriate way to transfer the Cursor back to the UI layer? I've seen many questions posed and there always seems to be someone suggesting there is a more appropriate way to do it. What is that way?

More specifically, I so far gather that the Activity should register as some sort of listener. When the Cursor is retrieved in the Service, a notification is set to the UI layer to access it. Where does the Service shove it so the Activity can grab it?

Also, my considered architecture is to have an extended Service, which is called by Activities. Inside this Service, database transactions will be made through the use of an extended ContentProvider, any listening Activities will be notified, and threads will be launched to hit web services. So, I have 1 extended Service, 1 extended ContentProvider and several extended Threads for different web services. Does this seem acceptable?

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

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

发布评论

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

评论(2

任谁 2024-09-23 13:23:21

我只是在活动中使用 ManagedQuery 调用来获取光标。这是一个相当轻的操作,我认为它不会影响用户界面。

我创建了一个服务,然后调用 Web 服务来查找新数据。如果找到新数据,则会将其放入我的数据库中并调用 sendBroadcast。我的 Activity 有一个已注册的 BroadcastReceiver,它会听到广播并在 Cursor 上调用 requery() 。

I am simply using the managedQuery call inside my Activity to get the Cursor. It's a fairly light operation and I don't think it will hold up the UI.

I've created a Service which then calls a web service to find new data. If new data is found, it is placed in my database and sendBroadcast is called. My Activity has a registered BroadcastReceiver which hears the broadcast and calls requery() on the Cursor.

酷炫老祖宗 2024-09-23 13:23:21

您是否考虑过实施 Loader?

我有一个应用程序,它使用 Intent 服务从服务器获取数据并存储
他们使用 ContentProvider。然后需要数据的片段或活动实现
LoaderCallbacksonCreateLoaderonLoadFinishedonLoaderReset)。

onCreateLoader 处,我实例化一个 CursorLoader 对象并返回它,而在 onLoadFinished 处,我确保来自 Cursor 的数据显示在我的 中片段/活动

查看官方加载器指南

Have you consider implementing Loaders?

I have an application which uses Intent services to fetch data from a server and store
them using a ContentProvider. Then the fragment or activity which needs the data implements
LoaderCallbacks<Cursor> (onCreateLoader, onLoadFinished and onLoaderReset).

At onCreateLoader I instantiate a CursorLoader object and return it while at onLoadFinished I make sure that the data from the Cursor are displayed in my Fragment/Activity.

Check the official loaders guide.

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