在React Native中不时进行后台作业

时间:2019-01-02 07:46:33

标签: android react-native-android

const mainStack = createStackNavigator({
  HomeScreen : {screen : Home},
  ViewScreen: {screen : ViewImages},
  ViewScreenServer: {screen : ViewImagesServer},
  FinalScreen: {screen : Final},

},{
  headerMode: 'none',
  navigationOptions:({navigation}) => ({
    header: null,
  }),

})

async connect = (data,tag) =>{
  RNFetchBlob.fetch('POST', 'http://192.168.0.76/survey/upload_image.php', {
    Authorization: "Bearer access-token",
    otherHeader: "foo",
    'Content-Type': 'multipart/form-data',
  }, [
      { name: 'image', filename: 'image.png', type: 'image/png', data: data },
      { name: 'image_tag', data: tag }
    ]).then((resp) => {
      //Alert.alert("succsessfully uploaded to server");
    }).catch((err) => {
     Alert.alert("Some Error occured;");
    })
}

const backgroundJob = {
 jobKey: "myJob",
 period: 11000,
 allowExecutionInForeground:true,
 allowWhileIdle: true,
 job: () => {
   Alert.alert("Transferring data to server");
   let realm = new Realm();
   let result = realm.objects('fields');
   for(let i = 0 ; i < result.length; i++){

                   this.connect(result[i].data,result[i].tag);

   }
    realm.write(() => {
     realm.delete(data)
   })
 }
};

BackgroundJob.register(backgroundJob);

var backgroundSchedule = {
 jobKey: "myJob",
 period: 11000
}

BackgroundJob.schedule(backgroundSchedule);

const AppNavigator = createAppContainer(mainStack)
export default AppNavigator;

上面的文件是AppNavigator.js文件,其中包含应用程序堆栈和应每11秒运行一次的后台作业。我仅将11秒用于测试目的,但以上代码不起作用,为什么?每11秒它应该运行该任务,但永远不会运行。我要做的是,当用户连接到Internet时,将所有数据从本地领域数据库移动到远程服务器上的mysql。

0 个答案:

没有答案