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

问题描述 投票:0回答:1

我有一个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}} <<原始类型:'对象'

我在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
)

但我的问题是,为什么我必须这样做?如果我确实修复它,还有另外两个地方也设置了同一个项目 - 一个已经有了包装,另一个没有。两者都有效,并且数据总是几乎相同 - 数字(即值)可能会发生变化,但它们在任何地方都是字符串键和数字值。

是什么让第一次写特别?

react-native react-native-ios asyncstorage
1个回答
0
投票

如果您使用AsyncStorage.setItem值应该是一个字符串。传递对象而不是字符串不应该工作!

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

REFERE DOCS HERE

© www.soinside.com 2019 - 2024. All rights reserved.