【问题标题】:How to optimize an hybrid Mobile App for slow internet connections如何针对慢速互联网连接优化混合移动应用程序
【发布时间】:2017-11-09 05:46:27
【问题描述】:

我有一个已开发的离子移动应用程序,但在互联网连接不佳的手机上使用时会引发很多连接错误。

有什么方法可以优化我的应用程序,使其在互联网连接不佳的设备上正常运行。即(GPRS 或 EDGE 连接)

【问题讨论】:

    标签: android ios ionic-framework hybrid-mobile-app


    【解决方案1】:

    您可以将您的 http 调用转换为包含更长的超时时间(此处为 3 分钟,单位为毫秒)。还要确保优雅地处理错误情况。

    this.http.get(url)
        .timeout(180000)
        .map(...)
        .subcribe(
            (success) => {...},
            (err) => { // Deal here with timeout error.
        }
    );
    

    还要确保缓存任何可以使用 ionic-storage 重复使用的数据,例如字体、图像、JSON 响应等。

    【讨论】:

      【解决方案2】:

      使用网络插件:

      https://ionicframework.com/docs/native/network/

      可以查看网络变化事件:

      // watch network for a disconnect
      let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
         console.log('network was disconnected :-(');
      });
      

      以及连接类型:

      function checkConnection() {
      var networkState = navigator.connection.type;
      
      var states = {};
      states[Connection.UNKNOWN]  = 'Unknown connection';
      states[Connection.ETHERNET] = 'Ethernet connection';
      states[Connection.WIFI]     = 'WiFi connection';
      states[Connection.CELL_2G]  = 'Cell 2G connection';
      states[Connection.CELL_3G]  = 'Cell 3G connection';
      states[Connection.CELL_4G]  = 'Cell 4G connection';
      states[Connection.CELL]     = 'Cell generic connection';
      states[Connection.NONE]     = 'No network connection';
      
      alert('Connection type: ' + states[networkState]);
      }
      

      也许你应该根据用户是否有连接和用户连接类型来管理你的请求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-12
        • 2015-08-19
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多