Angular 信息发布政策/策略

Angular 信息发布政策/策略,angular,ionic-framework,Angular,Ionic Framework,该应用程序用于交付软件包,每次交付软件包时,它都必须尽快将信息发布到服务器 因为有时邮递员没有互联网连接,所以会有某种类型的缓存来保存信息,并每1秒尝试发送一次信息 当然,这不是一个好主意,因为有时访问在服务器上失败,手机会因为永远重试而消耗大量电池和数据 我如何实施政策或策略来解决此问题 我的实际代码 $interval(function () { $this.syncup(); }, 1000); this.syncup = function ()

该应用程序用于交付软件包,每次交付软件包时,它都必须尽快将信息发布到服务器

因为有时邮递员没有互联网连接,所以会有某种类型的缓存来保存信息,并每1秒尝试发送一次信息

当然,这不是一个好主意,因为有时访问在服务器上失败,手机会因为永远重试而消耗大量电池和数据

我如何实施政策或策略来解决此问题

我的实际代码

$interval(function () {
            $this.syncup();
        }, 1000);


this.syncup = function () {
    $this.lastSync  = $filter('date')(new Date(), "mediumTime");
    $this.promises = $this.promises || [];
    if ($this.NotSentOperations() && $this.NotSentOperations().length && authenticationService.getToken()) {
        angular.forEach($this.NotSentOperations(), function (operation, key) {
            if (!$this.promises[operation.tarea]) {
                    $this.promises[operation.tarea] = $http.post(CONFIG.URL_POST_OPERATION, operation, {timeout: 300000})
                            .then(
                                    function (res) {
                                        $this.removeOperationNotSend($this.NotSentOperations().indexOf(operation));
                                    },
                                    function (error) {
                                        if (error.status === 409)
                                            $this.removeOperationNotSend($this.NotSentOperations().indexOf(operation));
                                    }
                            )
                            .finally(function () {
                                $this.promises[operation.tarea] = null; 
                            });
            }
        });
    }
};

是的,听起来像是有间隔,每1s调用一个方法是次优的。我建议使用“网络监视器”方法。我刚才这么做了,并用在这个答案中

基本思想是“知道”您的代码是在cordova还是web应用程序(pwa)上运行,然后使用相关的插件或浏览器方法,检测应用程序何时更改在线/离线状态。然后在脱机到联机状态更改期间进行同步这是针对角度5/IONAL 3的示例:

  // REQUIRED IMPORTS:
  import { Network } from "@ionic-native/network";
  import { Platform } from 'ionic-angular';

  // DETECT DEVICE/BROWSER:
  this.appIsOnDevice = !this.platform.url().startsWith('http');

  // INIT NETWORK MONITOR:
  initNetworkMonitor() {
    // check if we are on device or if its a browser
    if (this.appIsOnDevice) {
      // watch network for a disconnect
      this.disconnectSubscription = this.network
        .onDisconnect()
        .subscribe(() => {
          console.log("network disconnected:(");
          // do alert here
        });
      // watch network for a connection
      this.connectSubscription = this.network.onConnect().subscribe(() => {
        console.log("network connected!");
        // app got back online, do logic here
        if (this.network.type === "wifi") {
          console.log("we got a wifi connection, woohoo!");
        }
      });
    } else {
      this.browserOffline = Observable.fromEvent(window, "offline").subscribe(
        () => {
          // go offline logic here
        }
      );
      this.browserOnline = Observable.fromEvent(window, "online").subscribe(
        () => {
          // go back online
        }
      );
    }
  }

对不起,我的答案是Angular 5/Ionic 3+,您的代码可能使用angularjs Ionic 1?