AsyncStorage 似乎无法在我的物理设备上运行
AsyncStorage doesn't seems to work on my physical device
我的 react-native 应用程序在模拟器上运行得非常好。但是当我将它传输到实际设备时,来自 AsyncStorage 的回调似乎不起作用。我什至尝试发布 apk。但没有运气。
我正在像这样从文件 'Attraction.js' 中存储项目。
async addToRecentSearches(){
try{
var recentSearches = JSON.parse(await AsyncStorage.getItem("@travelGeo:recentSearches"));
var hasAMatch = recentSearches.find((el)=>{
return el.name.toLowerCase() === this.props.navigation.state.params.attraction.name.toLowerCase();
})
console.log("hasAMatch", hasAMatch);
// add the attraction to the AsyncStorage if the attraction doesnt already exist
if(!hasAMatch){
if(recentSearches && recentSearches.length===3){
/***push attraction and pop one***/
recentSearches.unshift(this.props.navigation.state.params.attraction);
recentSearches.pop();
AsyncStorage.setItem("@travelGeo:recentSearches", JSON.stringify(recentSearches) );
}else if(recentSearches && recentSearches.length<3){
/***push this attraction to the array***/
recentSearches.unshift(this.props.navigation.state.params.attraction)
AsyncStorage.setItem("@travelGeo:recentSearches", JSON.stringify(recentSearches) );
}else{
/***add this attraction to the storage***/
let arr = [];
arr.unshift(this.props.navigation.state.params.attraction);
AsyncStorage.setItem("@travelGeo:recentSearches", JSON.stringify(arr));
}
}
}catch(e){
console.log(e);
} }
我正在从 componentDidMount
调用这个异步函数
componentDidMount(){
this.addToRecentSearches();
}
然后我尝试像这样从不同的路径 (Weather.js) 检索数据。
async componentWillMount(){
try {
let recentSearches = JSON.parse(await AsyncStorage.getItem("@travelGeo:recentSearches"));
console.log(recentSearches);
this.setState({testGet:recentSearches[0].name})
for(var i=0;i<recentSearches.length;i++){
if(recentSearches[i].location){
let response = await getWeatherByLocation(recentSearches[i].location.latitude,recentSearches[i].location.longitude);
recentSearches[i].currentWeatherData = response;
}
}
console.log("recentSearchesWithWeatherData", recentSearches);
this.setState({recentSearches:recentSearches});
} catch (e) {
console.log(e);
}
}
(我在 componentWillMount() 中使用异步只是为了测试目的。)
我正在使用 {recentSearches:recentSearches}
状态来呈现数据。
此代码在模拟器中运行良好。但似乎不适用于实际设备。任何帮助将非常感激。
好吧,我是个白痴...这是一个非常愚蠢的错误。
错误是从这一行抛出的
var recentSearches = JSON.parse(await AsyncStorage.getItem("@travelGeo:recentSearches"));
我一直在尝试从尚未设置的异步存储中检索一个值。
然而,在模拟器中,当我第一次编写代码时,我必须设置 "@travelGeo:recentSearches"
键。因此,它确实可以在模拟器上运行,但不能在物理设备上运行。
我的 react-native 应用程序在模拟器上运行得非常好。但是当我将它传输到实际设备时,来自 AsyncStorage 的回调似乎不起作用。我什至尝试发布 apk。但没有运气。
我正在像这样从文件 'Attraction.js' 中存储项目。
async addToRecentSearches(){
try{
var recentSearches = JSON.parse(await AsyncStorage.getItem("@travelGeo:recentSearches"));
var hasAMatch = recentSearches.find((el)=>{
return el.name.toLowerCase() === this.props.navigation.state.params.attraction.name.toLowerCase();
})
console.log("hasAMatch", hasAMatch);
// add the attraction to the AsyncStorage if the attraction doesnt already exist
if(!hasAMatch){
if(recentSearches && recentSearches.length===3){
/***push attraction and pop one***/
recentSearches.unshift(this.props.navigation.state.params.attraction);
recentSearches.pop();
AsyncStorage.setItem("@travelGeo:recentSearches", JSON.stringify(recentSearches) );
}else if(recentSearches && recentSearches.length<3){
/***push this attraction to the array***/
recentSearches.unshift(this.props.navigation.state.params.attraction)
AsyncStorage.setItem("@travelGeo:recentSearches", JSON.stringify(recentSearches) );
}else{
/***add this attraction to the storage***/
let arr = [];
arr.unshift(this.props.navigation.state.params.attraction);
AsyncStorage.setItem("@travelGeo:recentSearches", JSON.stringify(arr));
}
}
}catch(e){
console.log(e);
} }
我正在从 componentDidMount
调用这个异步函数componentDidMount(){
this.addToRecentSearches();
}
然后我尝试像这样从不同的路径 (Weather.js) 检索数据。
async componentWillMount(){
try {
let recentSearches = JSON.parse(await AsyncStorage.getItem("@travelGeo:recentSearches"));
console.log(recentSearches);
this.setState({testGet:recentSearches[0].name})
for(var i=0;i<recentSearches.length;i++){
if(recentSearches[i].location){
let response = await getWeatherByLocation(recentSearches[i].location.latitude,recentSearches[i].location.longitude);
recentSearches[i].currentWeatherData = response;
}
}
console.log("recentSearchesWithWeatherData", recentSearches);
this.setState({recentSearches:recentSearches});
} catch (e) {
console.log(e);
}
}
(我在 componentWillMount() 中使用异步只是为了测试目的。)
我正在使用 {recentSearches:recentSearches}
状态来呈现数据。
此代码在模拟器中运行良好。但似乎不适用于实际设备。任何帮助将非常感激。
好吧,我是个白痴...这是一个非常愚蠢的错误。 错误是从这一行抛出的
var recentSearches = JSON.parse(await AsyncStorage.getItem("@travelGeo:recentSearches"));
我一直在尝试从尚未设置的异步存储中检索一个值。
然而,在模拟器中,当我第一次编写代码时,我必须设置 "@travelGeo:recentSearches"
键。因此,它确实可以在模拟器上运行,但不能在物理设备上运行。