gpt4 book ai didi

react-native - Redux-Persist 与 React-Native-Background-Fetch

转载 作者:行者123 更新时间:2023-12-03 23:52:08 34 4
gpt4 key购买 nike

我正在创建一个 React-Native 应用程序,它从 API 获取数据作为后台服务。

如果我可以使用后台任务期间获取的数据手动重新水化商店,我已经环顾了网络,但我找不到任何东西。

是否可以在应用程序被终止时从后台“服务”任务手动重新水化 redux-persist 存储?

最佳答案

对于仍然想知道的人来说,如果可以使用 react-native-background-fetch 来调度任何任务,只要它不接触 UI 就完全没问题,例如。 (AsyncStorage、Redux-Persist、Realm、DB...)与在 UI 中调用更改没有直接关系,因此完全可以使用。

在我的特定情况下,我使用最慢的选项 - AsyncStorage - 来保留我在全局应用程序级别使用的 Prop 类型的对象并将派生数据传递到我的组件:

// Example of HeadlessTask implementation

import BackgroundFetch from 'react-native-background-fetch'
import AsyncStorage from '@react-native-community/async-storage';

const HeadlessTask = async () => {

// Prepare data - fetching from API, other transformations...
let propsObject = {};

AsyncStorage.setItem(ITEM_KEY, JSON.strigify(propsObject))
.then(() => {
console.log('[AsyncStorage] Object Saved!');
// For iOS specifically we need to tell when the service job
// is done.
BackgroundFetch.finish();
})
.catch((e) => {
console.log('[AsyncStorage] Error saving object: ', e);
BackgroundFetch.finish();
});
}

附言请参阅 https://github.com/transistorsoft/react-native-background-fetch 以了解如何安装和实现后台提取。

关于react-native - Redux-Persist 与 React-Native-Background-Fetch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56119229/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com