【问题标题】:React Native AsyncStorage | Row too big to fit into CursorWindow反应原生异步存储 |行太大,无法放入 CursorWindow
【发布时间】:2019-11-22 15:31:20
【问题描述】:

我在ReactNative中使用AsyncStorage在设备上存储一些数据(大尺​​寸>2MB),然后用下面的代码读取它

try {
   const value = await AsyncStorage.getItem('date_stored_copy');
} catch (e) {
   console.log(e);
}

我收到以下错误:

行太大,无法放入 CursorWindow requiredPos=0, totalRows=1...

有什么方法可以增加 CursorWindow 的大小,或者 AsyncStorage 的其他替代方法吗?

【问题讨论】:

    标签: sqlite react-native asyncstorage


    【解决方案1】:

    我找到了另一个提到的替代方案here

    只需安装react-native-fs-store

    npm i react-native-fs react-native-fs-store
    
    react-native link react-native-fs
    

    并像这样使用它:

    import Store from "react-native-fs-store";
    const AsyncStorage = new Store('store1');
    

    它具有与 AsyncStorage 完全相同的 API,因此无需更改代码

    ** 请注意react-native-fs-storeAsyncStorage 慢,因为每个操作都会同步到文件。因此,您可能会在读取/写入数据时注意到延迟(屏幕无响应)

    【讨论】:

      【解决方案2】:

      另一种解决方案是将数据分成块然后写入。

      我编写了一个使用 AsyncStorage 的包装器,它正是这样做的:https://gist.github.com/bureyburey/2345dfa88a31e00a514479be37848d42

      请注意,它最初是为与 apollo-cache-persist(apollo-client 的持久性库)一起使用而编写的。 由于 graphql 将数据存储在一个非常扁平的结构中,这个解决方案开箱即用的效果很好。

      对于您的情况,如果您存储的对象如下所示:

      {
          data: { a lot of data here }
      }
      

      那么就没什么关系了,包装器也不起作用

      但是如果你的对象看起来像这样:

      {
          someData: { partial data },
          someMoreData: { more partial data },
          ....
      }
      

      那么理论上它应该可以工作。

      完全披露:我还没有彻底测试它,只将它与 apollo-cache-persist 一起使用

      【讨论】:

        【解决方案3】:

        我也遇到了这个问题,我是这样解决这个问题的:

        算法的基本描述:

        • “键”包含您的数据将被除以的部分数。 (例如:键是“MyElementToStore”,它的值是 7,表示您的数据需要拆分以适应 AsyncStorage 的一行中的每个部分)
        • 然后每个部分将作为单独的行存储在 AsyncStorage 中,方法是在键的名称后跟部分的索引。 (例如:["MyElementToStore0", "MyElementToStore1", ...]
        • 检索数据的工作方式相反,检索每一行并聚合到结果以返回
        • 清除存储的最后说明,在删除密钥之前删除每个部分很重要(使用最后一个函数“clearStore”以确保正确释放内存)

        AsyncStorage documentation

        import AsyncStorage from "@react-native-async-storage/async-storage";
        
        const getStore = async (key) =>
        {
          try
          {
            let store = "";
            let numberOfParts = await AsyncStorage.getItem(key);
            if(typeof(numberOfParts) === 'undefined' || numberOfParts === null)
              return null;
            else
              numberOfParts = parseInt(numberOfParts);
            for (let i = 0; i < numberOfParts; i++) { store += await AsyncStorage.getItem(key + i); }
            if(store === "")
              return null;
            return JSON.parse(store);
          }
          catch (error)
          {
            console.log("Could not get [" + key + "] from store.");
            console.log(error);
            return null;
          }
        };
        
        const saveStore = async (key, data) =>
        {
          try
          {
            const store = JSON.stringify(data).match(/.{1,1000000}/g);
            store.forEach((part, index) => { AsyncStorage.setItem((key + index), part); });
            AsyncStorage.setItem(key, ("" + store.length));
          }
          catch (error)
          {
            console.log("Could not save store : ");
            console.log(error.message);
          }
        };
        
        const clearStore = async (key) =>
        {
          try
          {
            console.log("Clearing store for [" + key + "]");
            let numberOfParts = await AsyncStorage.getItem(key);
            if(typeof(numberOfParts) !== 'undefined' && numberOfParts !== null)
            {
              numberOfParts = parseInt(numberOfParts);
              for (let i = 0; i < numberOfParts; i++) { AsyncStorage.removeItem(key + i); }
              AsyncStorage.removeItem(key);
            }
          }
          catch (error)
          {
            console.log("Could not clear store : ");
            console.log(error.message);
          }
        };

        【讨论】:

          猜你喜欢
          • 2020-02-07
          • 2021-06-09
          • 2019-01-28
          • 2021-12-07
          • 2020-07-09
          • 2019-11-28
          • 2021-12-08
          • 2020-11-23
          • 1970-01-01
          相关资源
          最近更新 更多