简体   繁体   English

AsyncStorage 不用于发布但用于调试(React Native)

[英]AsyncStorage not working on release but working on debug (React Native)

 "@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.我应该从我的 API 获取更新的数据,但似乎getData为空,而且它应该可以工作,因为我正在重新渲染组件。 Fetch is working fine because I can login . Fetch 工作正常,因为我可以登录。 the main problem i am having is the AsyncStorage .我遇到的主要问题是AsyncStorage Did someone encounter this kind of problem?有人遇到过这种问题吗?

You have a race condition where initializeData and getUserData will both be called on mount, and getData won't be set yet.您有一个竞争条件,其中 initializeData 和 getUserData 都将在挂载时调用,并且尚未设置 getData。

You can fix the race like this.你可以像这样修复比赛。

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

I saw it on my adb logcat that it can't store too much data我在我的 adb logcat 上看到它不能存储太多数据

So what I did is I chopped my data and store only the needed data .所以我所做的是切碎我的数据并只存储需要的数据。 That's what I did.这就是我所做的。

AsyncStorage 大小默认设置为 6MB,但是当它带有大量数据时,如果您想存储所有数据,则必须增加大小,您可以进入 android/gradle.properties 和 // 添加以下行 AsyncStorage_db_size_in_MB=10 或AsyncStorage_db_size_in_MB=15 取决于你必须存储的数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM