如何在React Native中使用AsyncStorage的multiGet方法检索数据

12

我正在考虑如何在写的文档中使用React-native的AsyncStorage multiGet功能:

AsyncStorage.multiGet(keys, (err, stores) => {

但是这些按键应该长什么样呢?以下是它们在我的应用程序中的设置方式:

AsyncStorage.multiSet([['@BarcodeList', JSON.stringify(scanedList)], ['@ScannedBarcode', gotCode]]);

没问题,但是我如何使用multiGet检索那些数据?使用getItem似乎可以工作,我错在哪里了?两者(getItem、multiGet)都在下面。

AsyncStorage.multiGet(["@BarcodeList", "@ScannedBarcode"]).then((scanedList2, scannedBarcode) => {
    //AsyncStorage.getItem("@BarcodeList").then((scanedList2) => {
1个回答

33

它的工作方式如下,因为它给出了嵌套数组响应

数组包含作为索引0作为索引1

 AsyncStorage.multiGet(["@BarcodeList", "@ScannedBarcode"]).then(response => {
            console.log(response[0][0]) // Key1
            console.log(response[0][1]) // Value1
            console.log(response[1][0]) // Key2
            console.log(response[1][1]) // Value2
        })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接