react-native-async-storage / async-storage

An asynchronous, persistent, key-value storage system for React Native.
https://react-native-async-storage.github.io/async-storage/
MIT License
4.63k stars 459 forks source link

AsyncStorage not working properly #219

Closed burhanahmed92 closed 4 years ago

burhanahmed92 commented 4 years ago

Current behavior

AsyncStorage not working properly on 0.60.5 I set and get the async storage with the help of two button. Value successfully sets and gets but when i reload the application and tried to get the value again without setting it return null.

Expected behavior

Async save the set value before and don't remove it until we manually set it to null. But now every time application is reloaded it forgets the saved async value and i have to set it again and again. Basically setup my authentication with asyncstorage, if user is signed in it shows dashboard else login screen. But now user login and open app again, it shows login screen again and again. It was working fine on Android 8.1 with React Native 0.59. But i upgraded my project to 0.60, its behavior changed.

Code

export default class LibTest extends Component {
    static navigationOption = {
        title: "LibTest", header: null, headerVisible: false
    };

    constructor(props) {
        super(props);
        this.state = {
            asyncVal: null,
            storedData: "myValue"
        }

    }

    async componentDidMount() {

    }

    storeData = async () => {
        console.log("inside storeData")
        try {
            await AsyncStorage.setItem('Test', 'TestValue')
        } catch (e) {
            console.log(e)
        }
    }

    getData = async () => {
        console.log("inside getData")
        try {
            const value = await AsyncStorage.getItem('Test')
            this.setState({ storedData: value })

        } catch (e) {
            // error reading value
        }
    }

    render() {
        return(
            <View style={{ marginTop: 40 }}>
                <Text> {this.state.storedData}</Text>
                <Button title={"storeData"} onPress={this.storeData}><Text>storeData</Text></Button>
                <Button title={"getData"} onPress={this.getData}><Text>getData</Text></Button>
            </View>
        )
    }
}

Environment

krizzu commented 4 years ago

@burhanahmed92 Do you get the warning about AsyncStorage being deprecated?

burhanahmed92 commented 4 years ago

@burhanahmed92 Do you get the warning about AsyncStorage being deprecated?

Yes i am getting the warning but i already update to latest version.... I don't know why its showing

krizzu commented 4 years ago

@burhanahmed92 Some of your dependencies might be still using it, hence condition race.

If using Community's AsyncStorage in dependencies is not possible, you can fallback to RN's AsyncStorage for the time being.

burhanahmed92 commented 4 years ago

@burhanahmed92 Some of your dependencies might be still using it, hence condition race.

If using Community's AsyncStorage in dependencies is not possible, you can fallback to RN's AsyncStorage for the time being.

I am having Issue only in Android Pie

Dallas62 commented 4 years ago

Hi!

Some of my users experience the same issue.

They made change, but when they restart the application, everything is "rollback", which is really strange.

On the first version already looks OK (read / write):

"@react-native-community/async-storage@^1.2.4":
  version "1.5.0"
  resolved "https://registry.yarnpkg.com/@react-native-community/async-storage/-/async-storage-1.5.0.tgz#647ffcd832272068b0be57332e08d73036ed391f"
  integrity sha512-2yE4RzQ5IL+UTPhuMY0ykNRKHf1m90jOnmp8fcDPUun5U97cXlorjI4p66ovDgF0FuOv8ZpiUKvunGy3qqBxwg==

Then I made an new release of the app with:

"@react-native-community/async-storage@^1.2.4":
  version "1.6.1"
  resolved "https://registry.yarnpkg.com/@react-native-community/async-storage/-/async-storage-1.6.1.tgz#19be07fc1d65c77bdeb20f46108ba12076aad1c5"
  integrity sha512-1WA28xfdhG+unkTEk/lXnqI2izv6belo0CYw7UdvaeHm8TIYT6eTmIIdGR7oiCa2xSKEnaPQqRMH6h7gyLNbww==

Then users start reporting bugs on Android. I read this article if-you-are-using-asyncstorage-react-native-api-you-need-these-2-permissions

I released a new version with WRITE_EXTERNAL_STORAGE, which was missing in the manifest, but nothing changed.

I'm not able to reproduce it on Emulator, so... 😢

Android Pie only 9.0.0 at the moment

Dallas62 commented 4 years ago

I doubled checked, but another user tells me on the first version (async-storage-1.5.0) that is not working, but some others have the problem after.

allyson7 commented 4 years ago

I'm having the same issue, it's a similar authentication with AsyncStorage in a learning application, monitoring with reactotron and android emulator, reactotron doesn't show the registry of setItem value and don't get the getItem value

krizzu commented 4 years ago

Right, thanks for reporting this. If someone can reproduce this with their app, can I ask you to run example app from this repo and see if it's happening as well?

I just ran it and I can see data being persisted correctly

Dallas62 commented 4 years ago

@allyson7 Can you provide some information on the configuration of your emulator ? Do you use "standard" AVD emulator ?

Dallas62 commented 4 years ago

@allyson7 @burhanahmed92 If you are able to reproduce this error, it would be great to take a look on: https://source.android.com/devices/tech/perf/compatibility-wal#disabling_compatibility_wal

You can apply it by creating a "config.xml" inside values folder and write <bool name="db_compatibility_wal_supported">false</bool>

allyson7 commented 4 years ago

@allyson7 Can you provide some information on the configuration of your emulator ? Do you use "standard" AVD emulator ?

I use genymotion, linux version, android 9.0 - api 28, standard template from genymotion samsung galaxy S9

Dallas62 commented 4 years ago

This error is not only for Android, it occures on iOS too, Users are mailing me about this, is there any way to fix it ?

faalkhah commented 4 years ago

I'm having the same issue too. Async Storage version: 1.6.2 React-Native version: 0.60.5

krizzu commented 4 years ago

I'd need some repo that is able to reproduce the issue, so I could have a look. Currently, I'm not able to reproduce it myself

zelenko commented 4 years ago

This issue was reported many times:

victorkurauchi commented 4 years ago

Not sure if still has the same issue, but i just upgraded versions and it's working the same way i used to do in the old version (0.55):

// react-native known issue: https://github.com/react-native-community/react-native-async-storage/issues/10
  async getItemSafely(key) {
    return new Promise((resolve, reject) => {
      AsyncStorage.getItem(key).then((value) => {
        // console.log(value);
        resolve(value);
      }, () => { // Couldn't read row 0, col 0 from CursorWindow
        console.log('failed');
        resolve({}); // Force not to break
      });
    });
  }
krizzu commented 4 years ago

If anyone still have this issue, please reopen with more info/repro steps.

fekajin commented 4 years ago

i have same issue.. when i set any value to null, its ok but after a refresh everything rolls back..

"react-native": "0.61.2", "@react-native-community/async-storage": "^1.6.1",

krizzu commented 4 years ago

when i set any value to null, its ok but after a refresh everything rolls back..

Setting values to null/undefined is not supported - probably the error is thrown there, which you don't handle. If you want to remove value, use .removeItem method

Ayanrocks commented 4 years ago

Why this issue is closed? The problem is not solved yet. Happening with the version1.8.1. After restarting the app it deletes the content stored in previous session.

darcoola commented 4 years ago

I have the same issue with version "@react-native-community/async-storage": "^1.9.0".`

My code is sth like: import AsyncStorage from '@react-native-community/async-storage'; [...] await AsyncStorage.setItem('token', response.data.token);

It works correctly, but when i reload my app using 'R' key AsyncStorage i cleared.

krizzu commented 4 years ago

The reason why it might be clearing up is mixing both AsyncStorage from Core and Community. Make sure you're using only one version

darcoola commented 4 years ago

This is the only place where I'm using it. I don't import AsyncStorage anywhere else in my project so there is no chance I mixed them. Here is my package.json:

{
  "name": "myproject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-community/async-storage": "^1.9.0",
    "@react-native-community/hooks": "^2.5.1",
    "@react-native-community/masked-view": "^0.1.7",
    "@react-navigation/bottom-tabs": "^5.2.5",
    "@react-navigation/native": "^5.1.4",
    "@react-navigation/stack": "^5.2.9",
    "axios": "^0.19.2",
    "i18n-js": "^3.5.1",
    "react": "^16.11.0",
    "react-native": "^0.62.1",
    "react-native-elements": "^1.2.7",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-localize": "^1.3.4",
    "react-native-reanimated": "^1.7.1",
    "react-native-safe-area-context": "^0.7.3",
    "react-native-screens": "^2.4.0",
    "react-native-vector-icons": "^6.6.0",
    "react-native-webview": "^9.1.0"
  },
  "devDependencies": {
    "@babel/core": "^7.9.0",
    "@babel/runtime": "^7.9.2",
    "@react-native-community/eslint-config": "^1.0.0",
    "babel-jest": "^25.2.6",
    "eslint": "^6.8.0",
    "jest": "^25.2.7",
    "metro-react-native-babel-preset": "^0.59.0",
    "react-test-renderer": "16.11.0"
  },
  "jest": {
    "preset": "react-native"
  }
}
krizzu commented 4 years ago

@darcoola Could be that one of your dependencies (or its dependencies) are using RN from Core. Do you see a warning about Async Storage being deprecated on app start?

darcoola commented 4 years ago

@Krizzu I don't see any warning concerning Async Storage, here is my console logs for starting the App:

npx react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 1142 file(s) to forward-jetify. Using 8 workers...
info JS server already running.
info Installing the app...

> Task :react-native-webview:compileDebugJavaWithJavac

> Task :app:stripDebugDebugSymbols UP-TO-DATE
Compatible side by side NDK version was not found.

> Task :app:installDebug
09:53:28 V/ddms: execute: running am get-config
09:53:28 V/ddms: execute 'am get-config' on 'emulator-5554' : EOF hit. Read: -1
09:53:28 V/ddms: execute: returning
Installing APK 'app-debug.apk' on 'Pixel_3_API_29(AVD) - 10' for app:debug
09:53:28 D/app-debug.apk: Uploading app-debug.apk onto device 'emulator-5554'
09:53:28 D/Device: Uploading file onto device 'emulator-5554'
09:53:28 D/ddms: Reading file permision of /Users/dkg_inn_mac/Desktop/Business_Link/business_link_mobile_app/android/app/build/outputs/apk/debug/app-debug.apk as: rw-r--r--
09:53:29 V/ddms: execute: running pm install -r -t "/data/local/tmp/app-debug.apk"
09:53:30 V/ddms: execute 'pm install -r -t "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : EOF hit. Read: -1
09:53:30 V/ddms: execute: returning
09:53:30 V/ddms: execute: running rm "/data/local/tmp/app-debug.apk"
09:53:30 V/ddms: execute 'rm "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : EOF hit. Read: -1
09:53:30 V/ddms: execute: returning
Installed on 1 device.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 14s
209 actionable tasks: 48 executed, 161 up-to-date
info Connecting to the development server...
info Starting the app on "emulator-5554"...
Starting: Intent { cmp=com.business_link_mobile_app/.MainActivity }

Is there a way to check if any dependency is using RN from Core? Or should I try to check it one by one removing from my project?

krizzu commented 4 years ago

I would init fresh project and add gradually add dependencies to check which one might be using core AS.

darcoola commented 4 years ago

@Krizzu I found bug in my code, thanks for your help, and sorry for trouble. The problem was that I checked the state in one of my Context which was not yet updated with the value stored in AsyncStorage while first rendered.

Glydric commented 3 years ago

I've the same problem, my app didn't work(white screen and no errors)

leogersen commented 3 years ago

i've the same issue, getItem returns always null...

 console.log(stringCredentials);// String... OK! 
        const store = async () => {
        try{
        await AsyncStorage.setItem('storedCredentials', stringCredentials)
        .then(x => AsyncStorage.getItem('storedCredentials')
        .then(console.log(x))); // always null
        console.log('Done!');

        } catch (error){ console.log(error);}   
        }
{
  "name": "app",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "@react-native-async-storage/async-storage": "^1.14.1",
    "@react-native-community/geolocation": "^2.0.2",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-navigation/bottom-tabs": "^5.11.8",
    "@react-navigation/native": "^5.9.3",
    "@react-navigation/stack": "^5.14.3",
    "axios": "^0.21.1",
    "base-64": "^1.0.0",
    "i": "^0.3.6",
    "metro-config": "^0.65.2",
    "react": "17.0.1",
    "react-native": "0.64.0",
    "react-native-button-spinner": "^0.1.4",
    "react-native-gesture-handler": "^1.10.3",
    "react-native-permissions": "^3.0.1",
    "react-native-reanimated": "^2.0.0",
    "react-native-safe-area-context": "^3.2.0",
    "react-native-screens": "^2.18.1",
    "react-native-svg": "^12.1.0",
    "react-native-svg-transformer": "^0.14.3",
    "react-native-swiper": "^1.6.0",
    "styled-components": "^5.2.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "@babel/runtime": "^7.12.5",
    "@react-native-community/eslint-config": "^2.0.0",
    "babel-jest": "^26.6.3",
    "eslint": "7.14.0",
    "jest": "^26.6.3",
    "metro-react-native-babel-preset": "^0.64.0",
    "react-test-renderer": "17.0.1"
  },
  "jest": {
    "preset": "react-native"
  }
}
akash-saha-1 commented 2 years ago

I am using this package and AsyncStorage always returns null after reopening /refresh of the expo app. "@react-native-async-storage/async-storage": "~1.15.0",

eddskt commented 1 year ago

I am using this package and AsyncStorage always returns null after reopening /refresh of the expo app. "@react-native-async-storage/async-storage": "~1.15.0",

same problem, and everything is implemented correctly, I've been programming for years, it was easier and it worked when it was the native version of async storage and didn't use EXPO, I believe it's a problem in the EXPO-CLI

chetanCyquent commented 1 year ago

Still same problem exist

React-native - 0.71.11 @react-native-async-storage/async-storage - 1.19.0 Android: 9 Device: Samsung Galaxy A8+(2018)

Anyone has any solution..