async-storage: Storage does not persist on app force close

What happened?

Storage does not persist when app force closed on iOS.

This issue was closed but is not resolved for me, nor is it resolved for others in that thread.

I am not using expo

Expected

  • Expected data to persist when saved with async storage.

Actual

  • Data only persists when app is refreshed or backgrounded
  • Data does not persist when app force closed

I’ve also tried delaying the loading of the storage with a setTimeout in case it was trying to load value before it could access storage, but there was no behavioral difference.

This is logged in xcode

Manifest does not exist - creating a new one.

So it seems on iOS at least a new manifest is created (and therefore not loading previously saved data) on start of app every time and that is what causes the value to be null

I do not have time to investigate android as deeply as I did iOS but there could be a similar problem there

Version

Have tried various between 1.17.3 and 1.18.1

What platforms are you seeing this issue on?

  • [] Android
  • iOS
  • macOS
  • Windows
  • web

System Information

System:
    OS: macOS 13.3.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 142.58 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.10.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
    Watchman: 2023.04.03.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.1, iOS 16.1, macOS 13.0, tvOS 16.1, watchOS 9.1
    Android SDK:
      API Levels: 25, 27, 28, 29, 30, 31, 33
      Build Tools: 28.0.3, 29.0.2, 30.0.1, 30.0.2, 30.0.3, 31.0.0, 33.0.0
      System Images: android-28 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom, android-30 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 2021.2 AI-212.5712.43.2112.8815526
    Xcode: 14.1/14B47b - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.13 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.0.0 => 18.0.0 
    react-native: 0.69.5 => 0.69.5 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps to Reproduce

Code

import AsyncStorage from "@react-native-async-storage/async-storage"

...

  public static async SAVE_STRING(key: string, value: string): Promise<boolean> {
    try {
      console.log("Saving key", key, value)
      await AsyncStorage.setItem(key, value)
      return true
    } catch {
      return false
    }
  }

  public static async LOAD_STRING(key: string): Promise<string | null> {
    try {
      console.log("Loading key", key)
      const value = await AsyncStorage.getItem(key)
      console.log("loaded value", value)
      return value
    } catch {
      console.log("Failed to load string")
      return null
    }
  }

Logs

 LOG  Saving key SAVED_USERNAME penguin

app is refreshed

 BUNDLE  ./index.js 

 LOG  Loading key SAVED_USERNAME
 LOG  loaded value penguin

app is forcce closed

 BUNDLE  ./index.js 

 LOG  Loading key SAVED_USERNAME
 LOG  loaded value null

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Here is my observation and fix. Hope this helps someone

My problem: Data stored is not persisted after reboot of the device. My OS: Android 13

AsyncStorage can be exported using the following two ways,

  1. import AsyncStorage from '@react-native-async-storage/async-storage';
  2. import {AsyncStorage} from 'react-native';

With 1 above, my data is lost after re-boot of the device. WIth 2, the data is persisted and it works as expected. However, with 2 above, I get a warning that “AsyncStorage would removed from react-native…”, Basically the console warning is suggesting to import using method 1 above and instead of 2.

I’ve decided to go ahead with 2 above for now and keep watching for fixes using 1.

This is still an issue on ```@react-native-async-storage/async-storage@^1.23.1```` after much troubleshooting, much nothing new has come out of the issue aside what has already been mentioned above

@krizzu issue is with one of our native modules, a minor release from them caused the problem. Of course its a highly obfuscated one so I don’t know what caused it. May report back later with info but closing it for now as its not an issue with the async storage package.

@TheHeumanModean This behavior (different paths for Dodcuments/Library data) exists since iOS 8, here’s the technical note on that. AsyncStorage uses system APIs to retrieve the application data location so that the manifest file can be stored there.

I’ve never used react-native/async-storage nor have I used this plugin pre 1.17.3.

My bet would be that one of your dependencies is using AsyncStorage and causing this - I can suggest scanning node_modules to see if any library is using AsyncStorage.