繁体   English   中英

React-Native更新AsyncStorage项目

[英]React-Native update AsyncStorage item

每当汽车价格改变时,我的应用程序都会向用户发送通知。 它是通过每小时运行一次的代码完成的。

代码如下:

  backgroundData(){
      BackgroundTimer.setInterval(() => {

        AsyncStorage.getItem('key').then(JSON.parse).then(items => {
          this.setState({value: items});
        })

        if(!this.state.value == 0 || !this.state.value == null){
          const promises = this.state.value.map((item, index) =>
            fetch(`URL/GetDetalhesViatura?CarID=${item[0]}`)
             .then(response => response.json())
          )
          Promise.all(promises).then(viaturas => this.setState({viaturas: flatten(viaturas)}))

          const newItem = []
          this.state.viaturas.map((item, index) =>
            newItem.push([item.Preco, item.CodViatura]),
            this.setState({ stateArray: newItem })
          )

          const newItem2 = []
          this.state.value.map((item, index) =>
            newItem2.push([item[1], item[0]]),
            this.setState({stateArray2: newItem2})
          )

          var results = new Array();
          //CODE TO CHECK ARRAYS FOR DIFFERENT PRICES
          this.setState({results: results})

          if(!this.state.results == 0 || !this.state.results == null){
            const promises = this.state.results.map((item, index) =>
              fetch(`URL/GetDetalhesViatura?CarID=${item[1]}`)
               .then(response => response.json())
            )
            Promise.all(promises).then(viaturas2 => this.setState({viaturas2: flatten(viaturas2)}))

            this.state.viaturas2.map((item, index) =>
              PushNotification.localNotification({
                message: "Notification",
              })
            )
          }
        } else {
          console.log('empty')
        }
      }, 9999999);
  }

首先,它在我的AsyncStorage上获取当前数据。 如果有数据,它将从API提取新数据,然后将这两个数据进行比较,如果价格不同,则会向用户发送通知。

问题是我需要用新价格更新每辆车的'key' (如果价格改变),否则应用程序将继续向用户发送通知。

我无法通过AsyncStorage得到我想要的东西,所以我不得不使用一个称为简单存储的包装器: https : //github.com/jasonmerino/react-native-simple-store

我不得不更改许多代码,但是后来我能够更改所需的数据并保存。

通过简单存储,它可以将每辆汽车以相同的键保存在单独的阵列或对象上,这很容易操纵数据。

暂无
暂无

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

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