Android MVP,可检查互联网连接 [英] Android MVP, where check internet connection

查看:68
本文介绍了Android MVP,可检查互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Andorid应用上实现MVP模式,我不确定检查互联网连接的最佳位置在哪里. 我通常会在进行任何网络呼叫之前检查是否有Internet连接.

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.

因此,我应该在活动演示者中检查它? 我认为Presenter是个不错的地方,因此它可以决定要做什么,但是我不确定100%是否应该将其放入活动中并避免致电给Presenter.

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.

推荐答案

我不认为Presenter是个好地方.演示者应从模型中查询新数据,例如getData().演示者不应该从本地数据库还是从服务器知道它.因此,在Presenter上检查Internet连接不是一个好主意.

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.

如果您使用存储库模式,那么Presenter将要求模型/存储库获取数据.该模型将首先将本地数据发送给演示者.并行地,它将发送服务器请求(如果存在网络连接)以下载新数据,并将新数据发送到Presenter.

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.

所以我认为,网络检查必须在存储库/模型中进行.您可能具有实现实际网络检查代码的Util类.然后从存储库中调用该方法,例如AppUtil.isNetworkConnectionAvailable();

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();

有关更多信息,请参见: https://github.com/googlesamples /android-architecture/tree/todo-mvp/

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

这篇关于Android MVP,可检查互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆