2017-02-23 49 views
2

我正在使用ionic 2 RC1並使用sublime作爲文本編輯器。我需要檢查網絡連接是否連接。所以爲此我使用離子原生Network來達到這個目的。但我面臨的問題與可觀察的Network.onDisconnect()。我已編輯initializeApp()方法,其中我檢查網絡連接並顯示連接斷開連接時的警報。在我寫了下面的代碼app.component.tsIONIC 2 native Network.onDisconnect()運行代碼兩次

showAlert(title, msg) { 
    let alert = this.alertCtrl.create({ 
     title: title, 
     subTitle: msg, 
     buttons: ['OK'] 
    }); 
    alert.present(); 
    } 

    initializeApp() { 
    this.platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     let disconnectSubscription = Network.onDisconnect().subscribe(() => { 
     this.showAlert("Error", "No internet connection"); 
     }); 
     StatusBar.styleDefault(); 
    }); 
    } 

我面臨的問題是,警報顯示兩次,如果應用程序從互聯網斷開。我在post中發現了類似的問題,但沒有得到答覆。任何有關這方面的幫助將不勝感激。 在此先感謝!

+0

你能解決這個問題嗎? – sebaferreras

+1

不,我沒有試圖解決它,因爲我已經轉移到其他基於Web的項目。我認爲這個問題應該已經在框架的新版本中解決了。 –

回答

5

爲了避免這種情況,你可以過濾的事件,只是做一些事情的時候,從在線狀態切換到離線,或從離線到在線(而不是每次活動由插件解僱) 。所以基本上你可以創建一個服務來處理這一切的邏輯是這樣的:

import { Injectable } from '@angular/core'; 
import { Network } from 'ionic-native'; 
import { Events } from 'ionic-angular'; 

export enum ConnectionStatusEnum { 
    Online, 
    Offline 
} 

@Injectable() 
export class NetworkService { 

    private previousStatus; 

    constructor(private eventCtrl: Events) { 
     this.previousStatus = ConnectionStatusEnum.Online; 
    } 

    public initializeNetworkEvents(): void { 
     Network.onDisconnect().subscribe(() => { 
      if (this.previousStatus === ConnectionStatusEnum.Online) { 
       this.eventCtrl.publish('network:offline'); 
      } 
      this.previousStatus = ConnectionStatusEnum.Offline; 
     }); 
     Network.onConnect().subscribe(() => { 
      if (this.previousStatus === ConnectionStatusEnum.Offline) { 
       this.eventCtrl.publish('network:online'); 
      } 
      this.previousStatus = ConnectionStatusEnum.Online; 
     }); 
    } 
} 

因此,我們的自定義事件(network:offlinenetwork:online)將只在連接真正改變(固定的情況下發射時多聯機或脫機事件當連接狀態完全沒有改變時由插件觸發)。

然後,在你的app.component文件,你只需要訂閱我們的自定義事件:

// Offline event 
this.eventCtrl.subscribe('network:offline',() => { 
    // ...    
}); 

// Online event 
this.eventCtrl.subscribe('network:online',() => { 
    // ...    
}); 
+0

這仍然是我的兩次火災 – Mike

+1

許多這些類型的解決方案提供了許多在線帖子回答這個問題。這絕對是最優雅的答案。然而,它不會像其他解決方案不工作(我已經嘗試過)。事件似乎同時觸發多次,或者觸發速度比上面的代碼可以執行和過濾網絡的多餘事件。 – JurgenW

+0

嗯......我在上面的代碼中使用了幾個應用程序,從來沒有遇到過這個問題。如果只有一個「NetworkService」實例,即使插件事件觸發非常快,它也應該由服務邏輯@JurgenW處理。 – sebaferreras

0

我已經解決了這個問題。人們遇到的真正問題是,在很多情況下,會創建多個Ionic頁面實例。因此,如果您在頁面上註冊了一個事件接收器,然後開始在App中來回導航,事件接收器將被多次註冊。 解決的辦法是添加事件接收器app.component.ts的intializeApp方法,像這樣:

// top of page 
import { Observable } from 'rxjs/Observable'; 

initializeApp() { 
    this.platform.ready().then(() => { 
     // Okay, so the platform is ready and our plugins are available. 
     // Here you can do any higher level native things you might need. 
     this.statusBar.styleDefault(); 
     this.splashScreen.hide(); 

     var offline = Observable.fromEvent(window, "offline"); 
     var online = Observable.fromEvent(window, "online"); 

     offline.subscribe(() => {   
      console.log('Offline event was detected.'); 
     }); 

     online.subscribe(() => { 
      console.log('Online event was detected.');   
     }) 
    }); 
    } 

兩個日誌消息將只觸發一次,當你去網上/離線分別無論怎樣你在App中導航的很多。

+0

*因此,如果您在頁面上註冊了一個事件接收器,然後開始在應用程序中來回導航,則事件接收器將多次註冊*僅當用戶在離開頁面時未取消訂閱事件(這是是錯誤的,但它與插件無關)。建議在'app.component.ts'文件或共享服務中處理這個問題,但是如果你在應用程序的任何頁面中**正確地做了**,它應該可以正常工作。 – sebaferreras

0

我認爲你應該使用下面的代碼來避免這個問題。

import { Network } from 'ionic-native'; 

    @Injectable() 
    export class NetworkService { 
    previousStatus:any 
     constructor() { 

     }  

    showAlert(title, msg) { 
     let alert = this.alertCtrl.create({ 
      title: title, 
      subTitle: msg, 
      buttons: ['OK'] 
     }); 
     alert.present(); 
     }   
      this.initializeApp(); 
      this.network.onDisconnect().subscribe(() => { 
       if (this.previousStatus === Online) { 
        this.showAlert("Error", "No internet connection"); 
       } 
       this.previousStatus = Offline; 
      }); 
      Network.onConnect().subscribe(() => { 
       if (this.previousStatus === Offline) { 
       this.showAlert("Alert", "Network was connected"); 
       } 
       this.previousStatus = Online; 
      }); 
     } 
    }