React-native: AsyncStorage Couldn't read row 0, col 0 from CursorWindow

Created on 22 Feb 2017  Â·  18Comments  Â·  Source: facebook/react-native

Description

When reading values that are too large using AsyncStorage on Android I run into this error:

I wrote a large value with AsyncStorage and later tried to read it, but it crashed.

BaseError: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
  at e (/node_modules/react-native/Libraries/Storage/AsyncStorage.js:423:22)
  at None (/node_modules/react-native/Libraries/Storage/AsyncStorage.js:416:71)
  at map ([native code])
  at errors (/node_modules/react-native/Libraries/Storage/AsyncStorage.js:416:51)
  at slicedToArray (/node_modules/react-native/Libraries/Storage/AsyncStorage.js:54:33)

Reproduction

It looks like on iOS we write to disk when the value is above a threshold and on Android we write to sqllite always.

let x = 'x';
while (x.length <= 3194304) {
  x = `${x}${x}`;
}
await AsyncStorage.setItem('@@GARBAGE@@', x); // works
const garbage = await AsyncStorage.getItem('@@GARBAGE@@'); // throws

Solution

In order for me to fix this I'll need to re-write AsyncStorage to write to disk instead of writing to sql. https://github.com/mvayngrib/react-native-safe-async-storage <--- this guys has the right idea but his code is super old and broken.

Additional Information

  • React Native version: 0.39.2
  • Platform: Android
  • Operating System: Samsung Galaxy S4
AsyncStorage Bug Help Wanted Locked

Most helpful comment

This is still a problem in the latest RN, AFAIK

All 18 comments

Hello! The react-native 0.29 is very out of date, can you try to repro this in the latest version?

Oh I'm sorry I meant to say 0.39.2 (Updated original post)

Did you find a solution? I've ran into this today as well. Storing base64 encoded images in AsyncStorage... on RN 0.41

Same error returned from Android. Solution might have to start writing to disk instead.

Yeah, I rewrote the library completely to use file storage instead of async
storage that uses sql. You should probably do the same. Check out
react-native-fs. Also base64 images is a pretty expensive operation,
consider writing image on disk directly, also using react-native-fs

On Fri, Mar 10, 2017 at 6:37 PM David Chan notifications@github.com wrote:

Did you find a solution? I've ran into this today as well. Storing base64
encoded images in AsyncStorage... on RN 0.41

Same error returned from Android. Solution might have to start writing to
disk instead.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/facebook/react-native/issues/12529#issuecomment-285835546,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAnYtJFGxrABi5m5egokfot4tNJHWosLks5rkgj0gaJpZM4MJTnn
.

Any recommended solution for this?? use react-native-fs?

@agrass react-native-fs is a step in the right direction but it's quite slow. https://github.com/mvayngrib/react-native-safe-async-storage has the right idea but it just needs to be re-written. Another option is to try to read from disk, if you fail catch the failure, dump your state, (full or partial) and keep moving on. I do both approaches in my app. Garbage collect, catch the failure, as well as I using react-native-fs and an approach like react-native-safe-async-storage

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

It's still an issue when trying to read something large from AsyncStorage.
Please consider an error on write instead.

also:
https://github.com/rt2zz/redux-persist/issues/284
https://github.com/robwalkerco/redux-persist-filesystem-storage
(redux-persist is not needed to use redux-persist-filesystem-storage)

@hramos It is not fixed for sure. Here is a small a explanation
https://stackoverflow.com/a/21432966/1410905.
Maybe it worth mentioning in the docs that max size for sqlite cursor is 2mb?

In docs said
On Android, AsyncStorage will use either RocksDB or SQLite based on what is available
would be great to know how to switch to RocksDB?

that limit of 2mb is for only one key/value document of the whole database?

per key/value

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

this still happening in newer versions right? I'm using RN 41

This is still a problem in the latest RN, AFAIK

Can confirm that this is still an issue on 0.53 - basically is still valid what @skv-headless linked and the solution proposed by @ThaJay is prob the best 'workaround' at the moment.

Only side-issue with using redux-persist-filesystem-storage is that it's a bit 'massive', meaning that it would reset all the saved data if deployed as an update on an existing app (as is in our scenario). Any ideas on how to implement it in a non-destructive way? (writing a migration or something?)

I've added this to a AsyncStorage.android.js file which I now use instead. I have a relatively large value to set.

Thanks to @robwalkerco

https://github.com/rt2zz/redux-persist/issues/284#issuecomment-291874254

/**
* @flow
*/

import RNFetchBlob from 'react-native-fetch-blob'

const DocumentDir = RNFetchBlob.fs.dirs.DocumentDir
const storagePath = `${DocumentDir}/persistStore`
const encoding = 'utf8'

const toFileName = (name: string) => name.split(':').join('-')
const fromFileName = (name: string) => name.split('-').join(':')

const pathForKey = (key: string) => `${storagePath}/${toFileName(key)}`

const AndroidFileStorage = {
  setItem: (
    key: string,
    value: string,
    callback?: ?(error: ?Error) => void,
  ) =>
    new Promise((resolve, reject) =>
      RNFetchBlob.fs.writeFile(pathForKey(key), value, encoding)
        .then(() => {
          if (callback) {
            callback()
          }
          resolve()
        })
        .catch(error => {
          if (callback) {
            callback(error && error)
          }
          reject(error)
        })
  ),
  getItem: (
    key: string,
    callback?: ?(error: ?Error, result: ?string) => void
  ) =>
    new Promise((resolve, reject) =>
      RNFetchBlob.fs.readFile(pathForKey(toFileName(key)), encoding)
        .then(data => {
          if (callback) {
            callback(null, data)
          }
          resolve(data)
        })
        .catch(error => {
          if (callback) {
            callback(error)
          }
          reject(error)
        })
  ),
  removeItem: (
    key: string,
    callback?: ?(error: ?Error) => void,
  ) =>
    new Promise((resolve, reject) =>
      RNFetchBlob.fs.unlink(pathForKey(toFileName(key)))
        .then(() => {
          if (callback) {
            callback()
          }
          resolve()
        })
        .catch(error => {
          if (callback) {
            callback(error)
          }
          reject(error)
        })
  ),
  getAllKeys: (
    callback?: ?(error: ?Error, keys: ?Array<string>) => void,
  ) =>
    new Promise((resolve, reject) =>
      RNFetchBlob.fs.exists(storagePath)
      .then(exists =>
        exists ? Promise.resolve() : RNFetchBlob.fs.mkdir(storagePath)
      )
      .then(() =>
        RNFetchBlob.fs.ls(storagePath)
          .then(files => files.map(file => fromFileName(file)))
          .then(files => {
            if (callback) {
              callback(null, files)
            }
            resolve(files)
          })
      )
      .catch(error => {
        if (callback) {
          callback(error)
        }
        reject(error)
      })
  ),
}

export default AndroidFileStorage

whilst AsyncStorage.ios.js

import {AsyncStorage} from 'react-native';
export default AsyncStorage;

Still happening, ussing

"react": "16.4.0",
"react-native": "0.55.4",

This issue has been moved to react-native-community/react-native-async-storage#10.

Was this page helpful?
0 / 5 - 0 ratings