React-native之持久化保存----AsyncStorage

AsyncStorage

  AsyncStorage是一个简单的,未加密的,异步的,持久化,关键值存储系统,是全局的。

iOS中存储类似于NSUserDefault,存储问plist文件存放在设备中。

Android中存储会在RocksDB 或者 SQLite 中,哪个可用就用哪个...

存数据

 /**
         * Sets value for key and calls callback on completion, along with an Error if there is any
         */
        setItem( key: string, value: string, callback?: ( error?: Error ) => void ): Promise<string>

e.g

try {
            AsyncStorage.setItem(
                'key',
                'I like to save it.',
                (error)=>{
                console.log(error);
            });
        } catch (error) {
            // Error saving data
            console.log(error);
        }

取数据

 /**
         * Fetches key and passes the result to callback, along with an Error if there is any.
         */
        getItem( key: string, callback?: ( error?: Error, result?: string ) => void ): Promise<string>

e.g

try {
            AsyncStorage.getItem(
                'key',
                (error,result)=>{
                    if (error){
                        console.log(error);
                    }
                    else
                    {
                        console.log(result);
                    }
                });
        }
        catch (error){
            console.log(error);
        }

 删数据

        removeItem( key: string, callback?: ( error?: Error ) => void ): Promise<string>

  

合并数据

/**
         * Merges existing value with input value, assuming they are stringified json. Returns a Promise object.
         * Not supported by all native implementation
         */
        mergeItem( key: string, value: string, callback?: ( error?: Error ) => void ): Promise<string>

  

清除数据

/**
         * Erases all AsyncStorage for all clients, libraries, etc. You probably don't want to call this.
         * Use removeItem or multiRemove to clear only your own keys instead.
         */
        clear( callback?: ( error?: Error ) => void ): Promise<string>

  

获得所有的key

/**
         * Gets all keys known to the app, for all callers, libraries, etc
         */
        getAllKeys( callback?: ( error?: Error, keys?: string[] ) => void ): Promise<string>

 

通过传入的多个key来获取匹配的value

 /**
         * multiGet invokes callback with an array of key-value pair arrays that matches the input format of multiSet
         */
        multiGet( keys: string[], callback?: ( errors?: Error[], result?: string[][] ) => void ): Promise<string>

 

通过传入多个键值对一起缓存

 

/**
         * multiSet and multiMerge take arrays of key-value array pairs that match the output of multiGet,
         *
         * multiSet([['k1', 'val1'], ['k2', 'val2']], cb);
         */
        multiSet( keyValuePairs: string[][], callback?: ( errors?: Error[] ) => void ): Promise<string>

  

删除所有的与key匹配的缓存数据

/**
         * Delete all the keys in the keys array.
         */
        multiRemove( keys: string[], callback?: ( errors?: Error[] ) => void ): Promise<string>

  

合并keyvalues

/**
         * Merges existing values with input values, assuming they are stringified json.
         * Returns a Promise object.
         *
         * Not supported by all native implementations.
         */
        multiMerge( keyValuePairs: string[][], callback?: ( errors?: Error[] ) => void ): Promise<string>
 

  

  

 

 

  

 

 

转载于:https://www.cnblogs.com/liuShanPei1024/p/5783197.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值