Redux-Persist 与 React-Native-Background-Fetch

P.Z*_*kov 5 react-native redux-persist

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

我浏览过网络是否可以使用后台任务期间获取的数据手动重新水化商店,但我找不到任何东西。

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

P.Z*_*kov 4

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

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

// 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();
        });
}
Run Code Online (Sandbox Code Playgroud)

PS 请参阅https://github.com/transistorsoft/react-native-background-fetch了解如何安装和实现后台 Fetch。