使用Cordova和PouchDB离线数据 [英] Offline data with Cordova and PouchDB

查看:754
本文介绍了使用Cordova和PouchDB离线数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Cordova编写一个字典应用程序。数据大小约为20MB。

I am writing a dictionary app with Cordova. The data size is about 20MB.

我希望应用程序完全脱机工作,并在本地查询数据。而且我发现PouchDB真的适合这样。

I want the app to work completely offline, and query data locally. And I find PouchDB really suitable for that.

但是,我也想要预先安装所有的数据(JSON格式)。也就是说,将所有的数据放在应用程序包中(在Cordova项目的www / data文件夹中)。

However, I also want to have all the data pre-installed with the app (in JSON format). That is, to put all the data in the app package (in www/data folder of the Cordova project).

从目前为止我知道的PouchDB从服务器创建数据或同步数据,但我想知道如何在应用程序的www文件夹中使用数据文件。

From what I've known so far, PouchDB can create data or sync data from a server, but I want to know how it can utilize data files inside the www folder of the app.

而且,我应该如何格式化数据文件,以便 PouchDB可以直接从中查询,而无需先从文件中读取,然后将数据插入PouchDB。

And, how should I format the data file so that PouchDB can query from it directly, without having to first read from the file and then insert data into PouchDB.

有没有人有什么想法?

Does anyone have any ideas? Or perhaps any other JS libraries that can accomplish this?

推荐答案

经过一些研究后,我想比较一下目前可能的选项:

After some research I want to compare some of the possible options at the moment:


  1. 直接使用PouchDB

  1. Use PouchDB directly

这是不实际的,因为:1)你必须在第一次启动时将数据插入PouchDB,这可能非常慢; 2)内存使用率很高,因为您将首先将所有数据加载到内存中(除非您拆分它们); 3)Cordova应用程序无法在后台运行;用户可以在插入数据时退出应用程序。

This is not practical because: 1) you have to insert data into PouchDB on the first startup, which can be extremely slow; 2) memory usage is high because you will first load all data into memory (unless you split them); 3) Cordova apps can't run in the background; user may exit the app while inserting data.

PouchDB的sqlite插件

PouchDB's sqlite plugin

您需要使用文件传输插件将数据复制到工作文件夹。

A possible solution. You need to use the File Transfer plugin to copy data into a 'working folder'.

缺点:


  • 有点麻烦,因为您需要另一个工作流程来创建sqlite文件。

  • PouchDB的sqlite适配器不能通过所有测试套件,并且可能会按照正式文档建议的速度降低。

  • 额外空间使用

将数据拆分为较小的json应用程序包中的文件(在/ www文件夹中)

Split data into smaller json files inside the app package (in /www folder)

这是我目前使用的。我把我的数据分成约100个较小的.json文件,并将它们放在/ www文件夹中。然后我可以使用XHR请求根据查询动态加载它们。

This is what I currently use. I split my data in to about 100 smaller .json files and put them inside /www folder. Then I can use XHR requests to load them dynamically based on query.

Pro:


  • 不需要额外的空间;只有一个数据副本

Con:


  • 丢失PouchDB的所有功能及其查询方法;它基本上是实现一个自定义只读数据库。

Lokijs

Lokijs是一个高性能的内存数据库。它可以从JSON加载数据库,或者(使用非官方插件)加载数据库

Lokijs is a high performance in-memory database. It can load database from JSON, or (using an unoffical plugin) load database from the Filesystem.

Con:


  • 因为Lokijs是一个内存数据库。内存使用可能是大数据大小和移动设备上的问题。






forward to是PouchDB的文件系统适配器。至少有人在想: https://github.com/pouchdb/pouchdb/issues/4631

预加载数据时出现问题:

预加载数据通常意味着将数据从应用程序包内复制到操作系统提供的数据目录(根据平台而不同)。

To preload data usually means to copy data from inside the app package to a data directory provided by the OS (different depending on platform).

经常涉及Cordova的文件插件和文件传输插件。
目前文件插件无法访问应用包内的数据,(虽然appDirectory在插件中定义)。您必须使用文件传输插件将数据文件从应用程序包复制到数据文件夹。

Cordova's File plugin and File Transfer plugin are often involved. Currently File plugin cannot access data inside app package, (although appDirectory is defined in the plugin). You have to use File Transfer plugin to copy your data files from the app package to data folder.

这篇关于使用Cordova和PouchDB离线数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆