xgrn-asyncstorage
v0.1.15
Published
RN容器-存储模块
Downloads
1
Readme
RNAsyncStorage
RN容器-存储模块
功能
存储模块
安装
私有库操作
nrm ls
如果提示nrm命令不存在,如下操作
npm install -g nrm
然后增加私有库地址并使用:
nrm add xgnpm http://172.16.2.71:4873/
nrm use xgnpm
npm install
安装依赖
$ npm i xgrn-asyncstorage -S
项目配置
Android
react-native link xgrn-asyncstorage
iOS
react-native link xgrn-asyncstorage
库升级
npm update xgrn-asyncstorage
配置
Android
使用库之前, 首先需要在rootProject
的build.gralde
内的项目依赖仓库中添加 maven { url "http://objectbox.net/beta-repo/" }
, 相关代码如下:
allprojects {
repositories {
mavenLocal()
jcenter()
google()
// 添加数据库的项目依赖maven地址
maven { url "http://objectbox.net/beta-repo/" }
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
}
}
在项目的application
的onCreate()
中初始化库的使用, 然后即可使用.
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
AsyncInitUtils.Companion.init(this, BuildConfig.DEBUG);
}
Native调用库用法
Native调用库用法, 可以调取AsyncStorageUtils
内的封装方法
|方法名 |含义 | |---------------------------------------------------------|:-------------------------------:| |addString(key: String, value: String, isEncrypt: Boolean)|添加字符串, isEncrypt表示是否加密| |addBoolean(key: String, value: Boolean) |添加布尔值(不加密)| |addDouble(key: String, value: Double)|添加Double(不加密)| |addInt(key: String, value: Int)|添加Int(不加密)| |addArray(key : String, value: ArrayList<*>, isEncrypt: Boolean)|添加队列, 可选是否加密| |addMap(key: String, value: HashMap<String, Any>, isEncrypt: Boolean)|添加Map, 可选是否加密| |setItem(key: String, value: Any?, isEncrypt: Boolean)|添加数据, 允许添加字符串, 布尔值, Double, Int, List, Map数据| |getItemValue(key: String) : Any?|获取对应key的值, 需要转为对应的类型| |getAllKeys(): Array?| 获取缓存所有的key| |clear(): Boolean| 清除所有的数据| |removeItem(key: String): Boolean| 删除对应key值的数据| |multiRemove(keys: ArrayList): Boolean|删除多key的数据|
js调用库用法
// 引入库
import XGAsyncStorage from 'XGAsyncStorage';
// 调用方法
// 随机添加或修改
_addString = ()=>{
XGAsyncStorage.setItem("1", "存存存啦啦啦", true)
.then((result)=>{console.warn("存string: " + this._showLog(result))})
};
// 随机添加布尔值
_addBoolean = ()=>{
XGAsyncStorage.setItem("2", true, true)
.then((result)=>{console.warn("存boolean: " + this._showLog(result))})
};
// 添加数字
_addNumber = ()=>{
XGAsyncStorage.setItem("3", 3.5, true)
.then((result)=>{console.warn("存number: " + this._showLog(result))})
};
// 添加对象
_addObject= ()=>{
const item = {};
item["text"] = "ddd";
item["age"] = 3;
item["double"] = 3.1;
item["boolean"]=true;
XGAsyncStorage.setItem("4", item, false)
.then((result)=>{console.warn("存对象: " + this._showLog(result))})
};
// 添加数组
_addArray= ()=>{
let item = [1,2,3,4];
XGAsyncStorage.setItem("5", item, true)
.then((result)=>{console.warn("存array: " + this._showLog(result))})
};
// 查询
_query = (key)=>{
XGAsyncStorage.getItem(key)
.then((result)=>{console.warn("查询: " + this._showLog(result))})
};
// 删除
_delete = ()=>{
XGAsyncStorage.removeItem("3")
.then((result)=>{console.warn("删除对应key值: " + this._showLog(result))})
};
// 多个删除
_multiRemove = ()=>{
const keys = ["1","2","3"];
XGAsyncStorage.multiRemove(keys)
.then((result)=>{console.warn("删除多个key值: " + this._showLog(result))})
};
// 删除所有
_clear = ()=>{
XGAsyncStorage.clear()
.then((result)=>{console.warn("清空缓存结果: " + this._showLog(result))})
};
// 查询所有的key值
_queryKey = ()=>{
XGAsyncStorage.getAllKeys()
.then((result)=>{console.warn("查询所有的key值: " + this._showLog(result))})
};
iOS
pod安装(由于用到了fmdb)
cd ios
pod init
vim Podfile
插入下面的代码
source 'http://git.ops.com/XGN-IOS/xgn.git'
source 'https://github.com/CocoaPods/Specs.git'
target 'XGAsyncStorageProject' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for XGAsyncStorageProject
pod "XGKeyValueStore"
end
找星云开通私有pod库权限,然后链接
pod repo add xgn http://git.ops.com/XGN-IOS/xgn.git
pod install
pod install(pod update)
用法
iOS
- 原生使用库文件方法
// 引入
#import <XGKeyValueStore/XGStore.h>
// 使用
- (IBAction)putString:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] putObject:@{
@"1": @"Yuanyuan"
} withEncrypted:YES];
NSLog(@"Put string %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)getString:(UIButton *)sender {
id value = [[XGStore shareInstance] getObjectForKey:@"1"];
NSLog(@"%@", value);
}
- (IBAction)putNumber:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] putObject:@{
@"3": @"10010232"
} withEncrypted:YES];
NSLog(@"Put number %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)getNumber:(UIButton *)sender {
id value = [[XGStore shareInstance] getObjectForKey:@"3"];
NSLog(@"%@", value);
}
- (IBAction)putBoolean:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] putObject:@{
@"2": @(YES)
} withEncrypted:YES];
NSLog(@"Put boolean %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)getBoolean:(UIButton *)sender {
id value = [[XGStore shareInstance] getObjectForKey:@"2"];
NSLog(@"%@", value);
}
- (IBAction)putObject:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] putObject:@{
@"4": @{
@"key": @"value",
@"haha": @100
}
} withEncrypted:YES];
NSLog(@"Put object %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)getObject:(UIButton *)sender {
id value = [[XGStore shareInstance] getObjectForKey:@"4"];
NSLog(@"%@", value);
}
- (IBAction)putArray:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] putObject:@{
@"5": @[@"item1", @"item2", @"item3"]
} withEncrypted:YES];
NSLog(@"Put array %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)getArray:(UIButton *)sender {
id value = [[XGStore shareInstance] getObjectForKey:@"5"];
NSLog(@"%@", value);
}
- (IBAction)deleteObjectByKey:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] removeObjectForKey:@"3"];
NSLog(@"Delete object %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)multiRemove:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] removeObjectsForKeys:@[@"1", @"2", @"3"]];
NSLog(@"Multi delete objects %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)removeAll:(UIButton *)sender {
BOOL isSuccess = [[XGStore shareInstance] clear];
NSLog(@"Clear %@.", isSuccess ? @"successed" : @"failed");
}
- (IBAction)getAllKeys:(UIButton *)sender {
NSArray *keys = [[XGStore shareInstance] allKeys];
NSLog(@"Get all keys: %@", keys);
}
- js调用库用法
// 引入库
import XGAsyncStorage from 'XGAsyncStorage';
// 调用方法
// 随机添加或修改
_addString = ()=>{
XGAsyncStorage.setItem("1", "存存存啦啦啦", true)
.then((result)=>{console.warn("存string: " + this._showLog(result))})
};
// 随机添加布尔值
_addBoolean = ()=>{
XGAsyncStorage.setItem("2", true, true)
.then((result)=>{console.warn("存boolean: " + this._showLog(result))})
};
// 添加数字
_addNumber = ()=>{
XGAsyncStorage.setItem("3", 3.5, true)
.then((result)=>{console.warn("存number: " + this._showLog(result))})
};
// 添加对象
_addObject= ()=>{
const item = {};
item["text"] = "ddd";
item["age"] = 3;
item["double"] = 3.1;
item["boolean"]=true;
XGAsyncStorage.setItem("4", item, false)
.then((result)=>{console.warn("存对象: " + this._showLog(result))})
};
// 添加数组
_addArray= ()=>{
let item = [1,2,3,4];
XGAsyncStorage.setItem("5", item, true)
.then((result)=>{console.warn("存array: " + this._showLog(result))})
};
// 查询
_query = (key)=>{
XGAsyncStorage.getItem(key)
.then((result)=>{console.warn("查询: " + this._showLog(result))})
};
// 删除
_delete = ()=>{
XGAsyncStorage.removeItem("3")
.then((result)=>{console.warn("删除对应key值: " + this._showLog(result))})
};
// 多个删除
_multiRemove = ()=>{
const keys = ["1","2","3"];
XGAsyncStorage.multiRemove(keys)
.then((result)=>{console.warn("删除多个key值: " + this._showLog(result))})
};
// 删除所有
_clear = ()=>{
XGAsyncStorage.clear()
.then((result)=>{console.warn("清空缓存结果: " + this._showLog(result))})
};
// 查询所有的key值
_queryKey = ()=>{
XGAsyncStorage.getAllKeys()
.then((result)=>{console.warn("查询所有的key值: " + this._showLog(result))})
};