使用Android应用程序部署大量静态数据

发布于 2024-11-29 18:38:30 字数 480 浏览 0 评论 0原文

我有一个 Android 应用程序需要离线工作并且需要大量静态数据。

目前,我正在 /res/raw 中使用 JSON 文件,并使用 Jackson 解析器将其加载到我的 POJO 方案中。它工作得非常好,因为我有一个外部程序将生成这些数据,并且偶尔当有更改时,我只需将新版本发布到市场,这样我就不必处理运行更新服务器和很快。

然而,现在我的 JSON 文件大约是 2.5MB,用于测试的数据集有限,最终大约是 5-10MB。

问题是解析文件已经需要大约 3-5 秒,并且每次重新启动应用程序时都需要完成此操作。

那么,我有什么选择呢?我可以将数据放入 sqlite 数据库,但这需要重写外部应用程序并更改相当多的数据结构。但后来我只能查询我现在需要的东西,而不能一次加载整个东西。

有更简单/更好的方法吗?另外,有没有一个好的方法来发布带有sqlite数据库的应用程序?我发现的所有文章都谈到在首次启动时为用户数据创建数据库,但这不是用户数据,我需要从市场部署它。

I have an Android app that needs to work offline and requires a lot of static data.

Currently I'm using a JSON file in the /res/raw and loading it with the Jackson parser into my POJO scheme. It works really well since I have an external program that will be generating this data and once in a while when there is a change I'll just publish new version to the Market so I don't have to deal with running an update server and so on.

However, right now my JSON file is about 2.5MB with limited dataset for testing, in the end it'll be about 5-10MB.

The issue is that it already takes about 3-5 seconds to parse the file and this needs to be done every time the application is restarted.

So, what are my options here? I could put the data to a sqlite database, but that would require rewriting the external application and changing the data structure quite a bit. But then I could only query the things I need at the moment and not loading the entire thing at once.

Is there some easier/better way? Also, is there a good way to publish the app with the sqlite database? All the articles I've found talk about creating the database for user data at first startup, but this is not user data and I need it to be deployed from the Market.

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

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

发布评论

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

评论(2

2024-12-06 18:38:30

JSON 感觉是一种错误的方法——它是一种对数据进行编码以进行传输的好方法,但仅此而已。

如果能够更多地了解您的应用程序的用途,那就太好了,但我很难想象一个用例,其中内存中拥有几 MB 的 POJO 是一个有效的解决方案。我认为使用 SQLite 会更好,这就是原因:

我可以将数据放入 sqlite 数据库,但这需要重写外部应用程序并更改相当多的数据结构。

您仍然可以使用其他程序的 JSON 输出,但不必使用 Jackson 将所有内容加载到 POJO 中,而是可以在第一次应用程序启动时填充数据库。这样,如果数据集不变,应用程序启动时间可以忽略不计。

如果您仍然想在应用程序的其余部分中使用 POJO,那么编写一个从数据库检索数据的查询并以与 Jackson 相同的方式创建对象就很简单了。

但是我只能查询我现在需要的东西,而不能一次加载整个东西。

您在做什么需要同时访问所有数据?为了实现同样的目的,搜索或排序一组对象总是比 SQL 查询慢。

另外,有没有好的方法可以使用sqlite数据库发布应用程序?

您绝对可以将您的应用程序与数据库一起发布,尽管我个人还没有这样做。这是一个相关问题:

默认情况下将一些数据加载到我们的数据库中sqlite

希望对您有所帮助。

JSON feels like the wrong approach for this - it's a good way to encode data to transfer, but that's pretty much it.

It'd be nice to have a bit more info on what exactly your app does, but I'm struggling to imagine a use-case where having several MB of POJOs in memory is an efficient solution. I think it'd be much better to use SQLite, and this is why:

I could put the data to a sqlite database, but that would require rewriting the external application and changing the data structure quite a bit.

You can still use your other program's JSON output, but instead of loading everything into POJOs with Jackson, you could populate the database on first app launch. This way, the app boot time is negligible if the dataset is unchanged.

If you still want to work with POJOs in the rest of your app, it'd be trivial to write a query that retrieved data from the database, and created objects in the same manner as Jackson.

But then I could only query the things I need at the moment and not loading the entire thing at once.

What're you doing that requires access to all the data at once? Searching or ordering a set of objects is always going to be slower than a SQL query to achieve the same thing.

Also, is there a good way to publish the app with the sqlite database?

You can definitely ship your app with a database, though I've not done so personally. This is a relevant question:

By Default load some data into our database sqlite

Hope that's of some help.

泛滥成性 2024-12-06 18:38:30

有一个出色的 API,称为 JExcel(只需 google 即可),可与 .xls 电子表格配合使用。如果您不打算进行任何选择而只是从源加载数据,我喜欢使用 JExcel,因为它更易于从桌面进行管理,并且生成更易于阅读的代码。

但是,不确定是否有任何性能差异。只是投入我的 2 美分:p

There's an excellent API called JExcel (just google it) that works with .xls spreadsheets. If you're not going to be doing any selecting and just loading data from a source, I like to use JExcel because it's more manageable from a desktop and produces easier-to-read code.

Not sure of any performance differences, however. Just throwing in my 2 cents :p

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