简体   繁体   English

带有RxAndroid + Retrofit的Android MVP

[英]Android MVP with RxAndroid + Retrofit

Recently I started reading a lot about MVP and I want to get into practicing my projects with it. 最近我开始阅读很多关于MVP的内容,我想用它练习我的项目。

However I am not able to correctly understand where should Rx + Retrofit code go? 但是我无法正确理解Rx + Retrofit代码应该去哪里? I think it should be in Model Layer via Interactors but still can someone share some light on this? 我认为应该通过Interactors在Model Layer中,但仍然可以有人对此有所了解吗?

Also what happens with the RX callback? RX回调会发生什么? the onNext(), onCompleted() and onFailure() passes data back to Presenter or do we implement listeners and then pass it on to Presenter? onNext(),onCompleted()和onFailure()将数据传递回Presenter,还是我们实现了侦听器,然后将其传递给Presenter?

I also want to persist data (Realm/StorIO) when I get it in onNext(), So again pass it to another DataLayer or where should it Go? 当我在onNext()中获取数据时,我还想保留数据(Realm / StorIO),所以再次将它传递给另一个DataLayer或者它应该去哪里?

Also should we decouple Rx callbacks further? 我们还应该进一步解耦Rx回调吗?

I am following this post https://davidguerrerodiaz.wordpress.com/2015/10/13/a-brief-introduction-to-a-cleaner-android-architecture-the-mvp-pattern/ 我关注这篇文章https://davidguerrerodiaz.wordpress.com/2015/10/13/a-brief-introduction-to-a-cleaner-android-architecture-the-mvp-pattern/

and this seperate github repo from antonioleiva.com https://github.com/antoniolg/androidmvp 这个来自antonioleiva.com的单独github回购https://github.com/antoniolg/androidmvp

As you pointed the RxJava functionality defines a use case of your model layer so it would be placed in an interactor of this layer. 正如您所指出的,RxJava功能定义了模型层的用例,因此它将被放置在该层的交互器中。 You can create a different interactor for each use case. 您可以为每个用例创建不同的交互器。 Let's say you are pulling a list of users from your server, this would be a use case and an interactor that will have the RxJava/Retrofit Observable. 假设您从服务器中提取用户列表,这将是一个用例和一个具有RxJava / Retrofit Observable的交互器。

Then you will have a Presenter with an Observer in it which is interested in this users list, so it will be subscribed to that Observable. 然后你会有一个带有Observer的Presenter,它对这个用户列表很感兴趣,所以它会被订阅到Observable。

And finally when this Observer in has all the data from the Observable (onCompleted), it will transform this data (if needed it) and pass to the View which will be just in charge of display it. 最后,当Observer in拥有来自Observable(onCompleted)的所有数据时,它将转换这些数据(如果需要的话)并传递给View,它将负责显示它。

There is awesome post explaining mvp. 有很棒的帖子解释mvp。 Rx is just additional tool for it. Rx只是它的附加工具。

http://hannesdorfmann.com/android/mosby-playbook/ http://hannesdorfmann.com/android/mosby-playbook/

there is deep explanation and source code with example. 有深度解释和源代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM