AsyncStorage在react-native-web上运行,在ios上失败(字符串化的json数据)

时间:2019-03-24 23:45:38

标签: react-native react-native-ios asyncstorage

我有一个Web / ios react-native项目。在网络上,这没有任何问题,但是在iOS上,我遇到了错误。奇怪的是,我在调试窗口中根本没有看到错误 ,所以我不得不依靠红色屏幕。

我保存数据的方式如下:

console.log(`user progress: writing string of >>${JSON.stringify(user.progress)}<< original's type: '${typeof user.progress}'`)
await AsyncStorage.setItem(
    appConfig.constants.graphcool.progress,
    user.progress
)

那条日志是我在调试器中看到的最后一条日志:

用户进度:编写>>>>“ VocabularyPairs”:{“ 1”:0.984375,“ 2”:0.996875,“ 3”:0.875}} <<原始字符串:'object'

我在iOS中出现红色屏幕并出现错误:

Exception '-[NSDictionaryM length]: unrecognized selector sent to instance 0x2832c5ce0' was thrown while invoking multiSet on target AsyncLocalStorage with params (
    (
        (
      progress,
            {
        VocabularyPairs =        {
          1 = "0.984375";
          2 = "0.996875";
          3 = "0.875";
        };
      }
    )
  ),
  311
)

所以-我可以通过更改代码来解决此问题:

await AsyncStorage.setItem(
    appConfig.constants.graphcool.progress,
    Platform.OS === 'ios' ? JSON.stringify(user.progress): user.progress
)

但是我的问题是,为什么我必须这样做?如果我确实进行了修复,那么还有另外两个地方也设置了相同的项目-一个已经有了该包装器,另一个没有该包装器。两者都起作用,并且总是几乎相同的数据-数字(即值)可能会更改,但是它们都是字符串键和数字值。

什么使第一次写变得特别?

1 个答案:

答案 0 :(得分:0)

如果您使用的是AsyncStorage.setItem,则值应为字符串。 传递对象而不是字符串不起作用!

  AsyncStorage.setItem(key: string, value: string, [callback]: ?(error: ?Error) => void)

REFERE DOCS HERE