目录
在移动端实际开发中难免会涉及涉及缓存:
a.AsyncStorage:一些简单文本、用户身份Token或者JSON文件等可以用AsyncStorage,类似h5的数据存储LocalStorage;在 iOS 上,AsyncStorage在原生端的实现是把较小值存放在序列化的字典中,而把较大值写入单独的文件。在 Android 上,AsyncStorage会尝试使用RocksDB,或退而选择 SQLite。
可参考:
https://blog.csdn.net/ahou2468/article/details/88954215
b.Realm:Realm不是ORM(对象关系映射数据库),也不是建立在sqlite之上。相反,我们为移动应用程序开发人员构建了一个完整的数据库,它使用动态映射到完整的自定义数据库引擎(而不仅仅是一个键值存储)的本机JavaScript对象。这允许我们在保持性能的同时提供一个简单的API。使用Realm,您可以建模复杂的数据,链接图形中的对象,以及组成高级查询。
可参考:
https://blog.csdn.net/ahou2468/article/details/94384048
c.文件存储(react-native-fs):
实现了react native的访问本地文件系统
主要介绍一下本地文件存储(react-native-fs);
注意事项:
对于rn<0.57和/或gradle<3,您必须安装react native fs的版本2.11.17!
对于rn>=0.57和/或gradle>=3,您必须时安装react native fs的版本2.13.2!
1.react-native-fs是什么?
实现了react native的访问本地文件系统;
2.react-native-fs支持哪些功能?
支持文件的创建、删除、查看、上传、下载;
3.react-native-fs如何使用?
a.安装(install)
npm install react-native-fs --save
b.链接(link)
react-native link react-native-fs
执行结果:
rnpm-install info Linking react-native-fs ios dependency
rnpm-install info Platform 'ios' module react-native-fs has been successfully linked
rnpm-install info Linking react-native-fs android dependency
rnpm-install info Platform 'android' module react-native-fs has been successfully linked
如果link失败,或者link之后仍旧不能import,则考虑手动link,详情查看官网教程,传送门。
4.react-native-fs功能介绍?
文件创建,修改,读取,删除,上传,下载 ;
5.文件操作说明
5.1文件目录说明
参数说明:
import RNFS from 'react-native-fs'
MainBundlePath (String) 主bundle目录的绝对路径; (在Android上不适合)
CachesDirectoryPath (String) 缓存目录的绝对路径;
ExternalCachesDirectoryPath (String) 外部缓存目录的绝对路径 (仅在Android上支持)
DocumentDirectoryPath (String) 文档目录的绝对路径
TemporaryDirectoryPath (String) 临时目录的绝对路径(返回到Android上的缓存目录)
LibraryDirectoryPath (String) NSLibraryDirectory绝对路径; (在iOS上支持)
ExternalDirectoryPath (String) 外部文件的绝对路径,共享目录(在android上支持)
ExternalStorageDirectoryPath (String) 外部存储的绝对路径,共享目录 (在android上支持)
Android打印日志:
MainBundlePath: undefined
CachesDirectoryPath: /data/user/0/com.helloworld/cache
ExternalCachesDirectoryPath: /storage/emulated/0/Android/data/com.helloworld/cache
DocumentDirectoryPath: /data/user/0/com.helloworld/files
ExternalDirectoryPath: /storage/emulated/0/Android/data/com.helloworld/files
ExternalStorageDirectoryPath: /storage/emulated/0
TemporaryDirectoryPath: /data/user/0/com.helloworld/cache
LibraryDirectoryPath: undefined
PicturesDirectoryPath: /storage/emulated/0/Pictures
FileProtectionKeys: undefined
android使用注意事项:
使用ExternalStorageDirectoryPath时,需要请求权限(在Android上)才能读取和写入外部存储;在Android23版本以后需要申请权限;申请权限示例:https://facebook.github.io/react-native/docs/permissionsandroid
IOS的打印日志
MainBundlePath: /Users/fandong/Library/Developer/CoreSimulator/Devices/2186A64F-
70D4-4BF2-A2F8-AE357F828D33/data/Containers/Bundle/Application/88D43269-12E3-4720-80D1-5618EBEF75DE/HelloWorld.app
CachesDirectoryPath: /Users/fandong/Library/Developer/CoreSimulator/Devices/2186A64F-70D4-4BF2-A2F8-
AE357F828D33/data/Containers/Data/Applicatio