后台/ x分钟后获取

问题描述 投票:0回答:1

我需要将我获取的数据保存并在redux中保存,以便在后台或x分钟内获取。

该应用程序有一个数据库,其记录不断更新(有时10到1小时),现在我必须杀死应用程序,以便再次获取日期。

我的代码中的所有内容都是基本的redux with action,并且使用Fetching API减少了5并调度数据。

export function fetchStatsFromAPI() {
  return (dispatch) => {
    dispatch(getStats())
    fetch(Config.SERVER_URL + '/mob/sport/getPlatfromStats', { //home
    method: 'POST',
    headers: {
        "Content-Type": "application/json",
    },
    body: JSON.stringify({
        code: "7",
        sessId: session,
        data: {
            client_id: client,
            u: user
        }
    })
    }).then((response) => response.json())
    .then((res) => {
        let data = res["data"]["response"]["data"];
  dispatch(getStatsSuccess(data))
  })
})
.catch(err => dispatch(getStatsFailure(err)))
}

其他部分只是基本的Redux和redux-thunk。

即使用户打开应用程序,我怎样才能使此抓取每隔X分钟运行一次?我尝试使用https://www.npmjs.com/package/react-native-background-fetch,但我不知道如何制作它。

react-native react-redux fetch-api redux-thunk
1个回答
1
投票

我的建议是在后端使用套接字,以便服务器可以在数据发生变化时向您的应用发送事件,您可以收听它并更新您的redux状态。如果你只想每x分钟获取一次数据,你可以在setTimeout中进行一次fetch调用。

`

componentDidMount(){
  this.timer=setInterval(()=>this.fetchData(),x*60*1000)
 }
fetchData() {
   //your fetch call
  }
componentWillUnmount(){
 clear(this.timer)
}
© www.soinside.com 2019 - 2024. All rights reserved.