Android AsyncTask - 每个数据库操作一个子类?

发布于 2024-10-07 11:20:15 字数 576 浏览 0 评论 0原文

我有一个初始化游戏的活动,它从多个 SQLite 表中进行多次选择和插入。

我试图理解 AsyncTask,但是,从到目前为止我读过的所有示例来看,我想知道是否必须为我需要执行的每个不同数据操作子类化 AsyncTask?

例如,我的 NewGame Activity 执行以下操作:

1) Insert new player record into PLAYER table
2) Insert new player's pet record into PET table
3) Select cursor of n records from INVENTORY
4) Insert array of ranomly chosen inventory items into PLAYER_OWNED table
5) ....more things of a similar nature

还会有更多的选择和插入用于各种事物,因此为每个事物都有一个单独的子类将会变得疯狂。更不用说这个游戏会有大约8个活动,都严重依赖数据库读写。

那么,基本上,我如何最好地使用 AsyncTask 来执行许多不同的 SQLite 操作?

I have an activity for initialising a game, that does multiple selects and inserts from a number of SQLite tables.

I'm trying to understand AsyncTask, but, from all the examples I've read so far, I'm wondering if I am going to have to subclass AsyncTask for every single different data operation I need to do?

For example, my NewGame Activity does the following:

1) Insert new player record into PLAYER table
2) Insert new player's pet record into PET table
3) Select cursor of n records from INVENTORY
4) Insert array of ranomly chosen inventory items into PLAYER_OWNED table
5) ....more things of a similar nature

There are going to be a few more selects and inserts for various things too, so having an individual subclass for each one is going to get crazy. Not to mention that there will be about 8 activities for this game, all relying heavily on database reads and writes.

So, basically, how do I best use AsyncTask to carry out a number of different SQLite operations?

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

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

发布评论

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

评论(1

昇り龍 2024-10-14 11:20:15

您可以将参数传递给 AsyncTask,更重要的是,如果您使用嵌套类,您可以使用 AsyncTask 类内部的全局变量,通过使用上述之一或两个提到的辅助工具,您应该能够使用同一个类并拥有它根据您传递的参数做不同的事情。我认为没有必要定义多个 AsyncTasks。

需要在每个活动中定义一个AsyncTask。

我写了需要,因为你真的不需要,但是这样做很舒服,而且很容易读/写代码,因为 AsyncTask 仅与活动相关联。当然,假设您使用嵌套类,我认为仅为 AsyncTask 编写单独的类文件没有意义。

You can pass parameters to a AsyncTask, even more, if you use nested clases, you can use global variables from inside the AsyncTask class, by using one of the above or both mentioned aids you should be able to use the same class and have it do diferent things depending on the parameter you pass. I see no real need to define multiple AsyncTasks.

You will need to define a AsyncTask in every activity.

I wrote need, because you really dont have to, but its comfortable to do it this way, and its easy to read/write code, as the AsyncTask is asociated to the activity only. This is of course suposing you use nested clases, I see no point in writing a separate class file just for an AsyncTask.

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