0

I want to calculate the duration of a ticket created when the app is in offline and display a toast message to sync the data within 24Hrs in the offline mode itself.

 static getDerivedStateFromProps(props, state) {
  if (!props.isOnline && !props.isInternetReachable) {
   let curr_time = moment().format();
   let offlineTicketCreatedTime = state.offlineNewTicketList.length > 0
          ? moment(state.offlineNewTicketList[0].Created).format()
          : 0;
   let difference = parseFloat(
        moment
          .duration(
            moment.utc(curr_time).diff(moment(offlineTicketCreatedTime))
          )
          .asSeconds()
      );
   if (difference > 1400) {
    alert("Sync now!!!");
   }
  }
return null; }

But this works only one time. I want to calculate the time in background even when I'm in other screens in offline mode. I want to run this calculation as a timer. And when the time is 23Hrs 50min a toast needs to be shown.

Vinay N
  • 249
  • 1
  • 8
  • 32

1 Answers1

0

Have a look at the later answers to this question How can I run background tasks in React Native?

Something like react-native-background-timer (or similar) sound like what is needed.

Besides that, might also want to store the start time in AsyncStorage so that it can be used to restart the timer if the app is killed and restarted.

shufflingb
  • 1,847
  • 16
  • 21