Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MVP, where check internet connection

Tags:

android

mvp

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 usually check if there is internet connection before doing any network call.

So, where should I check it in the Activity or in the Presenter? I think the Presenter would be a nice place, so it decides what to do, however I'm not 100% sure If I should place it in the activity and avoid doing a call to the Presenter.

like image 382
Jose M Lechon Avatar asked May 03 '16 15:05

Jose M Lechon


People also ask

How do I check my internet connection status?

Select the Start button, then type settings. Select Settings > Network & internet. The status of your network connection will appear at the top. Windows 10 lets you quickly check your network connection status.

How do I check my internet connection drops?

To test your Internet stability, you'll need a computer on your network capable of issuing a "ping" command and receiving a response. Place the computer outside of your firewall or turn off any software firewalls that may be installed. It's also best to test your Internet through a cable connection and not Wi-Fi.


1 Answers

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/

like image 115
Bob Avatar answered Dec 07 '22 07:12

Bob