mvvm rxswift_Rxswift MVVM-了解您的ViewModel

mvvm rxswift

It’s not enough by simply moving your logic code out of ViewController

仅仅将逻辑代码从ViewController中移出是不够的

As you may have known about MVVM architecture which has all the logics in ViewModel. Therefore, you can decouple from View and other services and be more focus. Here is the structure:

如您所知,MVVM体系结构具有ViewModel中的所有逻辑。 因此,您可以与View服务和其他服务分离,并获得更多关注。 结构如下:

Image for post

In this article, I’m focusing on the ViewModel. Yes, we have all the logic there but is that all we need? Apparently, the answer is NO. We have to do plenty of work to make the code simple, readable and testable.

在本文中,我将重点介绍ViewModel。 是的,我们拥有所有逻辑,但是我们所需的只是逻辑吗? 显然,答案是否定的。 我们必须做大量的工作才能使代码简单,可读和可测试。

To make the code readable and testable, we have to clearly confirm the input and output. Then we can mock the input and assert the output. It basically looks like this:

为了使代码易于阅读和测试,我们必须明确确认输入和输出。 然后,我们可以模拟输入并声明输出。 基本上看起来像这样:

Image for post
  • Input mainly refers to the user’s interaction or some automatic input happened when the view initialised (networking call) etc.

    输入主要是指用户的交互或视图初始化(网络调用)时发生的一些自动输入。

  • Output is the value or function bonded in the View. Whenever the value in Viewmodel changed, it will make some change in the View layer

    输出是绑定到视图中的值或函数。 每当Viewmodel中的值更改时,它将在View层中进行一些更改

  • Initialize is kind of special input as called: Dependency Injection. The ViewModel has to be initialised with something for further usage. By doing this, the ViewModel is telling the View:“ Hey, if you want to use me, get me the thing I need! ”

    初始化是一种特殊的输入,称为:依赖注入。 必须使用一些东西初始化ViewModel以便进一步使用。 这样,ViewModel告诉View:“嘿,如果您想使用我,请给我我需要的东西! ”

Now Let’s see how the “real-time” code structured with Rxswift:

现在,让我们看看如何使用Rxswift构建“实时”代码:

I believe this piece of code clearly expressed these 3 parts:

我相信这段代码清楚地表达了这三个部分:

  • We have ViewModelType protocol to implement the Input and Output

    我们有ViewModelType协议来实现输入和输出
  • We have the init() method with API manager to call the API data

    我们有带有API管理器的init()方法来调用API数据
  • In the Input: the reload could be a feature for the view to do pull to refresh by calling requestData in API manager

    在输入中:重新加载可能是视图的功能,可以通过在API管理器中调用requestData进行拉动刷新
  • In the Output: We have all the observable data that View need — articles, errors

    在输出中:我们拥有View需要的所有可观察数据-文章,错误

Here is the code in the View Layer:

这是视图层中的代码:

  • 1:We have dependency injection to initialize the ViewModel

    1:我们有依赖注入来初始化ViewModel
  • 2: set up the binding with article data

    2:设置与商品数据的绑定
  • 3: set up the binding with errors

    3:设置错误绑定
  • 4: trigger the input — reload to start the API call

    4:触发输入-重新加载以启动API调用

Please note: In Rxswift, the Drivers and Observables are lazy, they don’t do anything until they get subscribed.

请注意:在Rxswift中,Drivers和Observables是惰性的,它们只有订阅后才会执行任何操作。

After all the code works, how do we test it? Well, with this structure and TestSchedule in Rxswift, the test is becoming way easier to go.

在所有代码正常工作之后,我们如何对其进行测试? 好吧,有了Rxswift中的这种结构和TestSchedule,测试变得越来越容易。

We can have the scheduler to create the Observer binding to the output and Observable binding to the input. Then we mock the networking service to fail or success in order to test the asserted value. For more information, please consult:

我们可以让调度程序创建到输出的Observer绑定和到输入的Observable绑定。 然后我们模拟网络服务失败或成功,以测试所声明的值。 有关更多信息,请咨询:

结论: (Conclusion:)

Overall, when we are drafting our ViewModel, it better to explicitly point out the input and output functions/ variables. we can put it in the naming or use protocols.

总体而言,在起草ViewModel时,最好明确指出输入和输出函数/变量。 我们可以将其放在命名或使用协议中。

For the output/input. It should be our public/internal functions or observable variables as it has to interact with other objects. Then, we have all the private functions or other processor class/struct to handle most of the inner logic.

用于输出/输入。 它必须是我们的公共/内部函数或可观察变量,因为它必须与其他对象进行交互。 然后,我们拥有所有私有功能或其他处理器类/结构来处理大多数内部逻辑。

Thanks for reading! You can find a full demo HERE and feel free to leave any comment!

谢谢阅读! 您可以在此处找到完整的演示,并随时发表评论!

翻译自: https://medium.com/dev-genius/rxswift-mvvm-understand-your-viewmodel-50e8fe12436b

mvvm rxswift

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值