2011年开发iOS/Android HTML5离线存储解决方案
问题:
我需要一个与设备无关(例如 HTML5)的解决方案,用于在手机或平板电脑类型设备(例如 iOS/Android)上离线存储和查询 250,000 多行数据。我的想法是,我让人们在没有任何蜂窝数据连接的偏远地区工作,他们需要对此数据运行查询并在离线时对其进行编辑。部分它将基于地理位置,因此如果它们所在的区域有资产(使用 GPS),那么它将显示这些资产并让它们进行编辑。当他们返回办公室时,他们可以将数据同步回办公室服务器。
我从 Web 标准的角度来处理这个问题的原因基本上是为了节省金钱和时间,在 HTML5 中编写一次,然后它可以跨多个平台运行,而不是在 Objective C 和 Java 中编写两次。另外,如果你写的东西与平台无关,那么当每个人都转向更新的平台时,你就不会被锁定,也不会随波逐流。我们有一个为 Windows Mobile 5 编写的类似应用程序,但现在它已经没用了,因为该平台已经死了。
设备上的离线数据库需要:
- 快速(响应低于 2 秒)
- 潜在地执行联接并与其他表建立关系 能够查询数据库
- 选择特定范围或标准内的数据,例如通过 x & 。基于 GPS 读数的 y 坐标。
选项:
HTML5 本地存储:
适合小于 5,000 个键/值的少量数据,您甚至可以在其中存储数组/对象,如果您将其转换为 JSON。
缺点:
- 即使在高端计算机上,对于超过 10,000 行,浏览器也会 慢得像爬行一样。
- 无法对数据进行复杂的查询来提取所需的数据,因为您必须迭代整个存储并手动搜索它。
- 可存储的存储量的限制
Web SQL 数据库:
- 满足要求。
- 快速运行 250,000 行查询(1-2 秒)
- 可以创建复杂的查询、联接等
- 受 Safari、Android 和 Opera 支持,因此可在 iOS 和 Android 设备上运行
缺点:
- 自 2010 年 11 月起已弃用
- 安全性跨目录攻击的缺陷。这并不是真正的问题,因为我们不会使用共享托管
IndexedDB:
键/值对象存储类似于本地存储(除了索引之外)。
缺点:
- 对 200,000 行运行查询速度较慢(15-18 秒)
- 无法运行复杂查询
- 无法与其他表进行联接
- 不受主要手机或平板设备支持,例如 iPad/Android
- 标准版不支持完成
这留下了实现已弃用的 Web SQL 方法的唯一选择,该方法可能只能再工作一年左右。 IndexedDB和本地存储目前无法使用。
我不确定 Mozilla 和 Microsoft 是如何弃用 Web SQL 数据库标准的,以及 W3C 为何允许这种情况发生。据称,他们占据了桌面浏览器市场 77% 的份额。在高级移动设备上,Mozilla 和 Microsoft 的影响力几乎为零,而 Safari、Opera 和 Android 拥有超过 90% 的市场份额。 Mozilla 和 Mozilla 如何微软可以规定在移动市场中应该使用哪种标准,而在移动市场最有可能使用离线存储的地方没有任何意义。
在 Mozilla 的评论中,了解原因他们想要使用 IndexedDB 主要是出于“开发者审美”,并且他们不喜欢在 JavaScript 中运行 SQL 的想法。我不买。
目前提出的标准很差,而且是一个极其基本的 NoSQL 实现,速度很慢,甚至不支持人们在数据库中需要的高级功能。有很多样板代码来建立数据库并获取数据,但他们声称人们会在其之上编写一些不错的抽象库,以提供更高级的功能。截至 2011 年 10 月,它们已经不见踪影。
他们已经弃用了现有的 Web SQL 标准,该标准实际上可以在主要的移动/平板电脑浏览器中运行并实现。然而他们的“新”和“更好”标准在主要移动浏览器中不可用。
作为开发人员,我们应该在未来 3-5 年中使用什么,届时 IndexedDB 规范可能会实现标准化,拥有更多功能,在主要的移动/平板电脑浏览器中实现,并且有一些不错的库可以制作事情变得更容易?
W3C 应该保持 Web SQL 数据库标准并行运行并解决问题。它已经支持主要的移动平台并且运行得很好。事实上,Mozilla 和 Microsoft 作为桌面浏览器份额最多的两家公司能够废除这一标准,这一事实是相当可疑的,并且可以被视为试图阻碍移动网络平台的进步,直到他们能够赶上并提供与 iOS/Safari 和 Android 竞争的解决方案。
总之,有没有人能找到适用于 iOS/Android 手机/平板电脑设备的解决方案来解决我的问题。也许是一个很好的包装 API,可以在后台使用具有查询功能的多个数据库实现,并且它可以让您选择哪个数据库具有优先级。我见过类似 lawnchair 的东西,但我很确定它默认只允许您使用本地存储并回退到其他的。我想我宁愿它使用 Web SQL(默认情况下),然后使用较慢的选项。
非常感谢任何解决方案的帮助,谢谢!
The problem:
I need a device agnostic (e.g. HTML5) solution for storing and querying 250,000+ rows of data offline on a phone or tablet type device (e.g. iOS/Android). The idea being I have people working in remote areas without any cellular data connection and they need to run queries on this data and edit it while offline. Partly it will be geo-location based so if there are assets in the area they are in (uses GPS) then it will show those assets and let them be edited. When they return to the office they can sync the data back to the office server.
The reason that I'm approaching this from a web standard point of view is basically to save money and time by writing it once in HTML5 and then it works across multiple platforms rather than writing it twice in Objective C and Java. Also if you write something that's platform agnostic then you're not locked in and don't go down with the ship when everyone moves to a newer one. We had a similar app written for Windows Mobile 5, now it's useless as that platform is dead.
The offline database on the device needs to be:
- fast (responses under 2 seconds)
- potentially perform joins and have relationships with other tables able to query the database
- select data within a certain range or criteria e.g. by x & y co-ordinate based on the GPS reading.
Options:
HTML5 local storage:
Fine for small amounts of data <5,000 key/values, you can even store arrays/objects in it if you convert it to JSON.
Cons:
- For more than 10,000 rows even on a high end machine the browser will
slow to a crawl. - Can't do complex queries on the data to pull out the data you want as you have to iterate through the whole storage and manually search for it.
- Limitations with the amount of storage that can be stored
Web SQL Database:
- Meets the requirements.
- Fast to run a query on 250,000 rows (1-2secs)
- Can create complex queries, joins etc
- Supported by Safari, Android and Opera so will work on iOS and Android devices
Cons:
- Deprecated as of November 2010
- Security flaw with cross-directory attacks. Not really an issue as we won't be on shared hosting
IndexedDB:
Key/value object store similar to local storage except with indexes.
Cons:
- Slow to run a query on 200,000 rows (15-18secs)
- Can't run complex queries
- Can't do joins with other tables
- Not supported by main phone or tablet devices e.g. iPad/Android
- Standard not complete
This leaves the only option of implementing the deprecated Web SQL method which may only work for another year or so. IndexedDB and local storage are unusable at present.
I'm not sure how Mozilla and Microsoft got the Web SQL Database standard deprecated and why the W3C let it happen. Supposedly between them they have 77% of the desktop browser market. On advanced mobile devices Mozilla and Microsoft have nearly zero influence as Safari, Opera and Android have over 90% of the market share. How Mozilla & Microsoft can dictate which standard should be used in the mobile market which is where offline storage is most likely to be used doesn't make any sense.
In the comments from Mozilla about why they wanted to go with IndexedDB instead are mainly about 'developer aesthetics' and they don't like the idea of running SQL in JavaScript. I'm not buying it.
Currently the proposed standard is inferior and an extremely basic NoSQL implementation that is slow and doesn't even support the advanced features people need in a database. There is a lot of boilerplate code to establish the database and get data out but they claim people will write some nice abstraction libraries over the top of it that will provide more advanced features. As of Oct 2011 they're nowhere to be seen.
They've deprecated the existing Web SQL standard which actually works and is implemented in the main mobile/tablet browsers. Whereas their 'new' and 'better' standard is not available in the major mobile browsers.
What are we as developers supposed to use for the next 3-5 years which is when the IndexedDB specification might get around to being standardised, have more features, implemented in the main mobile/tablet browsers and there's some nice libraries to make things easier?
The W3C should keep the Web SQL Database standard running in parallel and just fix the issues. It already has support for the major mobile platforms and it works pretty well. The fact that Mozilla and Microsoft as the two players with the most desktop browser share were able to get this standard scrapped is pretty dubious and could be seen as an attempt to hinder progress on the mobile web platforms until they are able to catch up and offer competing solutions against iOS/Safari and Android.
In conclusion does anyone have a solution for my problem that will work for iOS/Android for phone/tablet devices. Maybe a nice wrapper API that can use multiple database implementations in the background with querying capability and it lets you choose which database has priority. I've seen things like lawnchair but I'm pretty sure it only lets you use local storage by default and falls back to the to the others. I think I'd rather it used Web SQL (by default) then the slower options.
Any help for a solution much appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议您查看 JayData 库,该库实际上的确切目的是为移动设备创建与存储无关的数据访问层。 JayData 提供了一个带有 JavaScript 语言查询 (JSLQ) 和 JavaScript CRUD 支持的抽象层,让您以完全相同的方式工作具有不同的离线和在线数据存储类型。 JayData 支持本地或远程处理复杂实体以及实体关系。
在撰写本文时,JayData 支持以下存储或协议:webSQL(sqLite)/IndexedDB/OData/YQL/FBQL。
不同系统提供不同存储引擎的特殊问题可以通过 JayData 的提供者回退功能轻松解决:它将使用它能找到的任何存储层,同时仍然向消费者代码提供相同的 API。
关于 WebSQL 在 2012 年被弃用:在撰写本文时,WebSQL 仍然拥有 95% 的设备覆盖率,包括三星 SmartTV 和亚马逊 Kindle。 查看 kindle 执行 WebSQL 单元测试与 JayData。
I would recommend checking out the JayData library, that actually has the exact purpose of creating a storage agnostic data access layer for mobile devices. JayData provides an abstraction layer with JavaScript Language Query (JSLQ) and JavaScript CRUD support and let's you work on the exact same way with different offline and online data store types. JayData supports dealing with complex entities and also entity relationships either locally or remotely.
At the time of writing JayData supports the following stores or protocols: webSQL(sqLite)/IndexedDB/OData/YQL/FBQL.
Your particular problem with different systems providing different storage engines can be easily addressed with the provider fallback feature of JayData: it will use whatever storage layer it can find while still provides the same API toward the consumer code.
With regarding WebSQL being deprecated by 2012: at the time of writing it is WebSQL that still has a 95% device coverage including Samsung SmartTV and amazon Kindle. Check out kindle executing WebSQL unit tests with JayData.
我会检查 CouchBase Lite。它是在 Android 和 iOS 上运行的 CouchDB 的近乎全功能的实现。
iOS
Android
如果您将应用程序包装在 PhoneGap 中,您可以为两个平台创建原生 HTML 5 应用程序您只需执行少量 Android/iOS 特定编程即可实现 CouchDB。
优点:
缺点:
I would checkout CouchBase Lite. It's a near full featured implementation of CouchDB that runs on Android and iOS.
iOS
Android
If you wrapped your App in something like PhoneGap you could create native HTML 5 apps for both platforms and you'd only have to do a tiny bit of Android/iOS specific programming to implement CouchDB.
Pros:
Cons:
在为我自己的项目寻找解决方案时,我做了更多的研究。
看起来这个库很有前途: http://nparashuram.com/IndexedDBShim/
它允许使用 IndexedDB API 在幕后具有 WebSQL。
它在最新的 iPad、iPhone 5、Android 4.2.2 上通过了测试。
希望这对某人有帮助。
I've made some more research while looking for a solution for my own project.
It looks like this library is rather promising: http://nparashuram.com/IndexedDBShim/
It allows to use IndexedDB API having WebSQL behind the scenes.
It's tests pass on recent iPad, iPhone 5, Android 4.2.2.
Hope this helps someone.
我会告诉你使用 Corona 。它是一个用于跨移动应用程序的私有平台,支持 SQLite 。
优点
缺点
我在这里粘贴他们对此的看法:
I would tell you to use Corona for it . It's a private Platform used for crossed-mobile applications which has support to SQLite .
Pros
Cons
I paste here what they say about it:
“我见过像 lawnchair 这样的东西,但我很确定它默认情况下只允许您使用本地存储,然后回退到其他存储。我想我宁愿它使用 Web SQL(默认情况下),然后使用较慢的选项”。
这是可配置的,存储引擎的每个“适配器”都是独立的,您可以将适配器传递给 Lawnchair 构造函数,或者,通过在以下情况下以不同方式连接 javascript 文件来更改它回退到其他存储选项的顺序:创建库。例如,对于indexed-db,然后回退到sqlite,然后gears sqlite:
当然,正如其他答案所建议的那样,您可以使用phonegap等将html5包装到本机应用程序中。那么您将有很多选择,但如果您想如果坚持 Web 标准,那么在我们广泛采用 IndexedDB 之前,这可能是一个好方法。
"I've seen things like lawnchair but I'm pretty sure it only lets you use local storage by default and falls back to the to the others. I think I'd rather it used Web SQL (by default) then the slower options."
This is configurable, each of the 'adapters' for storage engines is self contained, you can pass an adapter to the Lawnchair constructor, or, alternatively, change the order in which it falls back to other storage options by concatenating the javascript files differently when creating the library. e.g. for indexed-db then falling back to sqlite then gears sqlite:
Of course, as the other answers suggest, you can wrap your html5 into an native app using phonegap etc. then you'll have plenty of options, but if you want to stick to web standards then this may be a good way to go until we've got wide adoption of IndexedDB.
为什么不用 javascript 编写一个简单的存储引擎(其中涵盖了“基于标准”的部分)?显然你不需要任何非常花哨的东西,所以不应该花费太多的努力来让它工作。
我会执行以下操作:
这个解决方案只有在数据库足够简单的情况下才可行。但我认为它可能会起作用——JavaScript 支持在移动设备上很好。
为了获得灵感,这里是 JavaScript 中的 Btree+ 实现。
要读取本地文件,您需要文件 API,它可用于访问本地文件。大多数现代浏览器都支持它,甚至 Safari 6< /a>.不过,我无法确定当前的 iPhone 浏览器是否支持此 API。
Why not write a simple storage engine in javascript (which covers the "standards-based" part)? Apparently you don't need anything very fancy, so it should not take too much effort to have it working.
I would do the following:
This solution is only feasible if the database is simple enough. But I think it might work -- javascript support is good on mobile devices.
For inspiration here is a Btree+ implementation in javascript.
To read the local files you will need the file API, which can be used to access local files. It is supported in most modern browsers, even Safari 6. I have not been able to determine if current iPhone browsers support this API though.
值得查看我的开源库 https://bitbucket.org/ytkyaw/ydn- db/wiki/Home
作为 NoSQL 库,连接是手动的,但并非不可能。库中已经内置了关键的连接算法。
It worths to check out my open source library https://bitbucket.org/ytkyaw/ydn-db/wiki/Home
Being NoSQL library, join is manual, but not impossible. There is already key joining algorithms build-in the library.