Home > other >  AsyncStorage not working on release but working on debug (React Native)
AsyncStorage not working on release but working on debug (React Native)

Time:09-24

 "@react-native-async-storage/async-storage": "1.15.5",
 "react": "17.0.1",
 "react-native": "0.64.2",

Here's how I use my Async

fetch('',{
... //codes here
.then((response) => response.json())
      .then((responseJson) => {

     AsyncStorage.setItem('user_token', JSON.stringify(responseJson.token));
     AsyncStorage.setItem('user_id', JSON.stringify(responseJson.data));
     AsyncStorage.setItem('login_type', 'LocalLogin');
     
   });
});

then after that here's how I get my asyncstorage

const [getData, setGetData] = useState();

const initializeData = async() => {
    const userToken = await AsyncStorage.getItem('user_id');
    if(userToken)
    {
      setGetData(JSON.parse(userToken));
    }
  }

  useEffect(() => {
    initializeData();
  },[]);

useEffect(() => {
    getUserData();
,[navigation,isFocused]);

const getUserData = async() => {
 var formBody = JSON.stringify({email:`${getData.email}`});
 fetch('',{ ... //more codes here
}

I should get the updated data from my API but it seems like the getData is null and also it should work because I am re rendering the components. Fetch is working fine because I can login . the main problem i am having is the AsyncStorage . Did someone encounter this kind of problem?

CodePudding user response:

You have a race condition where initializeData and getUserData will both be called on mount, and getData won't be set yet.

You can fix the race like this.

useEffect(() => {
    if (getData) getUserData();
,[navigation,isFocused,getData]);

CodePudding user response:

I saw it on my adb logcat that it can't store too much data

So what I did is I chopped my data and store only the needed data . That's what I did.

  • Related