私の反応ネイティブアプリは、エミュレータで正常に動作します。しかし、実際のデバイスに転送すると、AsyncStorageからのコールバックは機能していないようです。私もapkをリリースしようとしました。運がない。AsynStorageが物理デバイスで動作しないようです
私は、このようなファイル '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}
状態を使用しています。
このコードはエミュレータで正常に動作します。実際のデバイスでは動作しないようです。どんな助けでも大歓迎です。