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>