Android MVP, where check internet connection

后端 未结 2 1947
我寻月下人不归
我寻月下人不归 2020-12-31 03:55

I\'m implementing MVP pattern on an Andorid App and I have a doubt about where is the best place for checking the internet connection. I us

相关标签:
2条回答
  • 2020-12-31 04:27

    Solution:-

    You should check the internet connection availablity in BaseActivity class and then extend that activity is the better practice i think.

    I do in my project like this:-

    public boolean isInternetAvailable() {
        return internet.isAvailable();
    }
    
    0 讨论(0)
  • 2020-12-31 04:40

    I dont think Presenter is a good place. Presenter should ask the new data from the model, like getData(). Presenter should not know whether its from local database or from server. So checking the internet connection at the Presenter will not be a good idea.

    If you use the Repository pattern, the Presenter will ask the model/repository to get the data. The model will send the local data to the presenter first. Parallely, it will send server request(if there is network connection) to download new data, and send the new data to the Presenter.

    So I think, the network check must be at the Repository/ model. You could have Util class which implements the actual network check code. And call that method from repository, like AppUtil.isNetworkConnectionAvailable();

    For more info, refer: https://github.com/googlesamples/android-architecture/tree/todo-mvp/

    0 讨论(0)
提交回复
热议问题