后台/获取 x 分钟后



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

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

我的代码中的所有内容都是带有操作的基本 redux,使用 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 但我不知道如何制作。

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

'

componentDidMount(){
  this.timer=setInterval(()=>this.fetchData(),x*60*1000)
 }
fetchData() {
   //your fetch call
  }
componentWillUnmount(){
 clear(this.timer)
}

最新更新