react-native-background-task预计在UI线程上运行

时间:2018-04-24 11:44:56

标签: react-native react-native-android

我正在尝试使用在线api离线同步数据捕获,我定期使用react-native-background-task运行后台任务来检索脱机数据并使用在线api同步数据。

react-native-background-task error



// This component below triggers the background task on load 

import { sync, clean } from "../../services/market/forms/tasks";
import MediaWorker from "../../services/market/forms/MediaWorker";

let worker = new MediaWorker();

BackgroundTask.define(async () => {
  console.log("Life's good");
  
  // loads data from db and sync them with the online service
  await sync(worker);
  
  // delete synced data from the db and end task
  await clean();
});

export default class Onboard extends Component {
  constructor(props) {
    super(props);
  }

  async checkStatus() {
    const status = await BackgroundTask.statusAsync();

    if (status.available) {
      // schedule the background task
      BackgroundTask.schedule();
      return;
    }

    const reason = status.unavailableReason;
    if (reason === BackgroundTask.UNAVAILABLE_DENIED) {
      Alert.alert(
        "Denied",
        'Please enable background "Background App Refresh" for this app'
      );
    } else if (reason === BackgroundTask.UNAVAILABLE_RESTRICTED) {
      Alert.alert(
        "Restricted",
        "Background tasks are restricted on your device"
      );
    }
  }

  componentDidMount() {
    this.checkStatus();
  }

  render() {
   // Not important for the question
  }
}

// snippet for sync function
export const sync = async worker => {
  const formInstances = await loadFormInstance();

  if (formInstances.length) {
    // Send Textual data
    const formInstancesText = filterFormInstances(formInstances, "text");
    postFormTextInstance(formInstancesText);

    // Get form image data and post
    const formInstancesImage = filterFormInstances(formInstances, "image");
    formInstancesImage.forEach(worker.send);
    // Get form audio data and post

    const formInstancesAudio = filterFormInstances(formInstances, "audio");
    formInstancesAudio.forEach(worker.send);

    // Get form video data and post
    const formInstancesVideo = filterFormInstances(formInstances, "video");
    formInstancesVideo.forEach(worker.send);
  } else {
    console.log("Nothing to sync");
    BackgroundTask.finish();
  }
};


// snippet for clean function
export const clean = async () => {
  const formInstances = await loadFormInstance();
  if (formInstances.length) {
    const toBeDeleted = new Set();
    formInstances.forEach(formInstance => {
      const fields = formInstance.fields;
      let allSynced = true;
      for (let index in fields) {
        const field = fields[index];
        if (field.synced === false) {
          allSynced = false;
          break;
        }
      }
      if (allSynced) {
        toBeDeleted.add(formInstance.instanceID);
      }
    });

    toBeDeleted.forEach(deleteFormInstance);
  } else {
    console.log("All tasks finished");
    BackgroundTask.finish();
  }
};



 Adb log(Used for monitoring background activity)

注意:后台任务成功运行了很长时间,但是当应用程序在调试模式下构建时,有时会显示红色屏幕。 在发布模式下,应用程序完全崩溃。

Stack trace generated by Crashlytics in production

1 个答案:

答案 0 :(得分:0)

我修复了它,结果是react-native-background-task版本与我的react-native版本不兼容,我从0.48.1升级到0.51.0,需要反应16.0.0