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

AsyncStorage

  AsyncStorage是一個簡單的,未加密的,異步的,持久化,關鍵值存儲系統,是全局的。javascript

iOS中存儲相似於NSUserDefault,存儲問plist文件存放在設備中。java

Android中存儲會在RocksDB 或者 SQLite 中,哪一個可用就用哪一個...json

存數據swift

 /**
         * 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);
        }

取數據app

 /**
         * 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);
        }

 刪數據this

        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>

  

清除數據spa

/**
         * 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>
 
相關文章
相關標籤/搜索