使用React Native时,我有哪些存储数据的选项? (iOS和Android) [英] What are my options for storing data when using React Native? (iOS and Android)

查看:96
本文介绍了使用React Native时,我有哪些存储数据的选项? (iOS和Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是React Native世界的新手,通常也是移动/本机世界的新手,我发现文档在数据持久性方面有点缺乏。



我有什么选择在Re​​act Native中存储数据以及每种类型的含义?例如,我看到有本地存储和异步存储,但后来我也看到像Realm这样的东西,我很困惑所有这些都适用于外部数据库。



我特别想知道:




  • 数据持久性有哪些不同的选项?

  • 对于每个,持久性的限制是什么(即数据何时不再可用)?例如:关闭应用程序,重新启动手机等时。

  • 对于每个,iOS和Android之间的实现是否存在差异(除了常规设置)?

  • 如何比较离线访问数据的选项? (或者离线访问通常如何处理?)

  • 我应该记住还有其他注意事项吗?



感谢您的帮助!

解决方案

以下是我在确定前进的最佳方法时所学到的知识我目前的几个应用项目。



异步存储(内置到React Native)



我使用AsyncStorage作为正在制作的应用。存储保留在设备的本地,未加密(如另一个答案中所述),如果您删除应用程序就会消失,但应保存为设备备份的一部分并在升级期间保留(通过CodePush进行原生升级ala TestFlight和代码升级) )。



结论:本地存储;您提供自己的同步/备份解决方案。



SQLite



其他项目我曾经使用sqlite3进行应用程序存储。这为您提供了类似SQL的体验,可压缩的数据库也可以与设备进行传输。我没有将它们同步到后端的任何经验,但我想象存在各种库。有用于连接SQLite的RN库。



数据以传统的数据库格式存储,包括数据库,表格,键,索引等,所有这些都以二进制格式保存到磁盘。可以通过命令行或具有SQLite驱动程序的应用程序直接访问数据。



结论:本地存储;您提供同步和备份。



Firebase



Firebase提供等等事实上,一个实时的noSQL数据库以及一个JSON文档存储(如MongoDB)意味着保持1到n个客户端的同步。文档讨论了脱机持久性,但仅针对本机代码(Swift / Obj-C,Java)。 React Native使用的Google自己的JavaScript选项(Web)不提供缓存存储选项(请参阅下面的2/18更新)。该库是在假设Web浏览器将要连接的情况下编写的,因此将存在半持久连接。您可以编写本地缓存机制来补充Firebase存储调用,或者您可以在本机库和React Native之间编写桥梁。



[更新2/2018]此后我发现了 React Native Firebase ,它为本机iOS和Android库提供兼容的JavaScript界面​​(做谷歌可能做的事情/应该已经完成​​了),给你本土图书馆的所有好处,并获得React Native支持的奖励。随着谷歌在实时数据库旁边引入了一个JSON文档存储,我正在为Firebase提供一个很好的第二次查看我计划构建的一些实时应用程序。



实时数据库存储为类似JSON的树,您可以在网站上编辑并简单地导入/导出。



结论:使用react-native-firebase,RN可以获得与Swift和Java相同的优势。 [/ update]适用于网络连接设备。低利用率的低成本。与其他Google云产品完美结合。数据在界面上易于查看和编辑。



境界



也是真实的具有自动网络同步的时间对象存储。他们称自己为设备优先,演示视频显示设备如何处理零星或有损网络连接。



他们提供您托管的免费版本的对象存储在您自己的服务器上或AWS或Azure等云解决方案中。您还可以创建不与设备持久存储的内存存储,不与服务器同步的仅设备存储,只读服务器存储以及跨一个或多个设备同步的完整读写选项。他们拥有专业和企业选项,每月费用比Firebase高。



与Firebase不同,React Native和Xamarin支持所有Realm功能,就像它们在Swift / ObjC / Java(本机)应用程序中一样。



您的数据与代码中的对象相关联。因为它们是已定义的对象,所以您确实具有模式,并且版本控制是代码完整性的必要条件。可通过Realm提供的GUI工具直接访问。设备上的数据文件是跨平台兼容的。



结论:设备优先,可选同步免费和付费计划。 React Native支持的所有功能。水平缩放比Firebase贵。



iCloud



老实说,我没有做了很多玩这个,但将在不久的将来这样做。



如果你有一个使用CloudKit的本机应用程序,你可以使用CloudKit JS从Web应用程序(或者在我们的示例中为React Native)连接到应用程序的容器。在这种情况下,您可能拥有原生iOS应用程序和React Native Android应用程序。



与Realm一样,它会在本地存储数据并在可能的情况下将其同步到iCloud。每个客户都有适用于您的应用和私人商店的公共商店。客户甚至可以选择与其他用户共享他们的一些商店或对象。



我不知道访问原始数据是多么容易;模式可以在Apple的网站上设置。



结论:非常适合以苹果为目标的应用程序。



Couchbase



大牌,背后有很多大公司。有一个社区版和企业版,标准的支持成本。



他们在他们的网站上有一个教程,用于连接到React Native。我也没有花太多时间在这个上,但它在功能方面看起来是Realm的可行替代品。我不知道在您的应用程序或您构建的任何API之外获取数据是多么容易。





<结论:看起来具有与Realm类似的功能。可以是仅设备或同步。我需要尝试一下。



MongoDB



我正在使用这个服务器端,用于在本地使用AsyncStorage的应用程序。我喜欢将所有内容存储为JSON对象,使得向客户端设备的传输非常简单。在我的用例中,它被用作电视指南数据的上游提供商和我的客户端设备之间的缓存。



数据没有硬结构,如模式,因此每个对象都存储为一个易于搜索,可过滤等的文档。类似的JSON对象可以有额外的(但不同的)属性或子对象,允许你在构造对象/数据方面有很大的灵活性。



我还没有尝试过任何客户端到服务器同步功能,我也没有用它嵌入。确实存在针对MongoDB的React Native代码。



结论:仅本地NoSQL解决方案,没有明显的同步选项,如Realm或Firebase。






自从我最初写这篇文章以来,我花了一些时间在Realm上。我喜欢我不必编写API来同步应用程序和服务器之间的数据,类似于Firebase。无服务器函数对这两个函数看起来也非常有用,限制了我必须编写的后端代码量。



我喜欢MongoDB数据存储的灵活性,所以正在成为基于Web和其他连接所需应用程序的服务器端的选择。



我找到了 RESTHeart ,它创建了一个非常简单,可扩展的RESTful MongoDB的API。构建一个React(Native)组件,读取和写入RESTHeart,然后将它们传递给MongoDB或从MongoDB传递它们不应该太难。






我添加了有关数据存储方式的信息。有时,如果您需要调整和测试数据,了解在开发和测试期间您可能需要做多少工作是很重要的。


I am still new in the React Native world, and generally in the mobile/native world as well, and I am finding the documentation a bit lacking when it comes to data persistence.

What are my options for storing data in React Native and the implications of each type? For instance, I see that there is local storage and async storage, but then I also see things like Realm, and I'm confused how all of this would work with an outside database.

I specifically want to know:

  • What are the different options for data persistence?
  • For each, what are the limits of that persistence (i.e., when is the data no longer available)? For example: when closing the application, restarting the phone, etc.
  • For each, are there differences (other than general setup) between implementing in iOS vs Android?
  • How do the options compare for accessing data offline? (or how is offline access typically handled?)
  • Are there any other considerations I should keep in mind?

Thanks for your help!

解决方案

Here's what I've learned as I determine the best way to move forward with a couple of my current app projects.

Async Storage ("built-in" to React Native)

I use AsyncStorage for an in-production app. Storage stays local to the device, is unencrypted (as mentioned in another answer), goes away if you delete the app, but should be saved as part of your device's backups and persists during upgrades (both native upgrades ala TestFlight and code upgrades via CodePush).

Conclusion: Local storage; you provide your own sync/backup solution.

SQLite

Other projects I have worked on have used sqlite3 for app storage. This gives you an SQL-like experience, with compressible databases that can also be transmitted to and from the device. I have not had any experience with syncing them to a back end, but I imagine various libraries exist. There are RN libraries for connecting to SQLite.

Data is stored in your traditional database format with databases, tables, keys, indices, etc. all saved to disk in a binary format. Direct access to the data is available via command line or apps that have SQLite drivers.

Conclusion: Local storage; you supply the sync and backup.

Firebase

Firebase offers, among other things, a real time noSQL database along with a JSON document store (like MongoDB) meant for keeping from 1 to n number of clients synchronized. The docs talk about offline persistence, but only for native code (Swift/Obj-C, Java). Google's own JavaScript option ("Web") which is used by React Native does not provide a cached storage option (see 2/18 update below). The library is written with the assumption that a web browser is going to be connecting, and so there will be a semi-persistent connection. You could probably write a local caching mechanism to supplement the Firebase storage calls, or you could write a bridge between the native libraries and React Native.

[Update 2/2018] I have since found React Native Firebase which provides a compatible JavaScript interface to the native iOS and Android libraries (doing what Google probably could/should have done), giving you all the goodies of the native libraries with the bonus of React Native support. With Google's introduction of a JSON document store beside the real-time database, I'm giving Firebase a good second look for some real-time apps I plan to build.

The real-time database is stored as a JSON-like tree that you can edit on the website and import/export pretty simply.

Conclusion: With react-native-firebase, RN gets same benefits as Swift and Java. [/update] Scales well for network-connected devices. Low cost for low utilization. Combines nicely with other Google cloud offerings. Data readily visible and editable from their interface.

Realm

Also a real time object store with automagic network synchronization. They tout themselves as "device first" and the demo video shows how the devices handle sporadic or lossy network connectivity.

They offer a free version of the object store that you host on your own servers or in a cloud solution like AWS or Azure. You can also create in-memory stores that do not persist with the device, device-only stores that do not sync up with the server, read-only server stores, and the full read-write option for synchronization across one or more devices. They have professional and enterprise options that cost more up front per month than Firebase.

Unlike Firebase, all Realm capabilities are supported in React Native and Xamarin, just as they are in Swift/ObjC/Java (native) apps.

Your data is tied to objects in your code. Because they are defined objects, you do have a schema, and version control is a must for code sanity. Direct access is available via GUI tools Realm provides. On-device data files are cross-platform compatible.

Conclusion: Device first, optional synchronization with free and paid plans. All features supported in React Native. Horizontal scaling more expensive than Firebase.

iCloud

I honestly haven't done a lot of playing with this one, but will be doing so in the near future.

If you have a native app that uses CloudKit, you can use CloudKit JS to connect to your app's containers from a web app (or, in our case, React Native). In this scenario, you would probably have a native iOS app and a React Native Android app.

Like Realm, this stores data locally and syncs it to iCloud when possible. There are public stores for your app and private stores for each customer. Customers can even chose to share some of their stores or objects with other users.

I do not know how easy it is to access the raw data; the schemas can be set up on Apple's site.

Conclusion: Great for Apple-targeted apps.

Couchbase

Big name, lots of big companies behind it. There's a Community Edition and Enterprise Edition with the standard support costs.

They've got a tutorial on their site for hooking things up to React Native. I also haven't spent much time on this one, but it looks to be a viable alternative to Realm in terms of functionality. I don't know how easy it is to get to your data outside of your app or any APIs you build.

[Edit: Found an older link that talks about Couchbase and CouchDB, and CouchDB may be yet another option to consider. The two are historically related but presently completely different products. See this comparison.]

Conclusion: Looks to have similar capabilities as Realm. Can be device-only or synced. I need to try it out.

MongoDB

I'm using this server side for a piece of the app that uses AsyncStorage locally. I like that everything is stored as JSON objects, making transmission to the client devices very straightforward. In my use case, it's used as a cache between an upstream provider of TV guide data and my client devices.

There is no hard structure to the data, like a schema, so every object is stored as a "document" that is easily searchable, filterable, etc. Similar JSON objects could have additional (but different) attributes or child objects, allowing for a lot of flexibility in how you structure your objects/data.

I have not tried any client to server synchronization features, nor have I used it embedded. React Native code for MongoDB does exist.

Conclusion: Local only NoSQL solution, no obvious sync option like Realm or Firebase.


[Edit] I've spent some time with Realm since I originally wrote this. I like how I don't have to write an API to sync the data between the app and the server, similar to Firebase. Serverless functions also look to be really helpful with these two, limiting the amount of backend code I have to write.

I love the flexibility of the MongoDB data store, so that is becoming my choice for the server side of web-based and other connection-required apps.

I found RESTHeart, which creates a very simple, scalable RESTful API to MongoDB. It shouldn't be too hard to build a React (Native) component that reads and writes JSON objects to RESTHeart, which in turn passes them to/from MongoDB.


[Edit] I added info about how the data is stored. Sometimes it's important to know how much work you might be in for during development and testing if you've got to tweak and test the data.

这篇关于使用React Native时,我有哪些存储数据的选项? (iOS和Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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