具有HTTP请求的ReactiveCocoa 4 MVVM最佳实践

时间:2021-10-14 20:15:41

I'm working on the login part of my app and I thought of using ReactiveCocoa 4. :)

我正在处理我的应用程序的登录部分,我想使用ReactiveCocoa 4. :)

This is in my view's init:

这是我认为的初始化:

self.viewModel.loginSignal = self.LoginButton.rac_signalForControlEvents(UIControlEvents.TouchUpInside)!

self.viewModel.loginStatus.producer.startWithNext({ status in
  self.setLoginButtonStatus(status)
})

self.viewModel.initSignals()

Where setLoginButtonStatus will just disable/enable the button, etc. and status is just an enum.

其中setLoginButtonStatus将仅禁用/启用按钮等,状态只是一个枚举。

And this is in my view model's initSignals()

这是我的视图模型的initSignals()

self.loginSignal!.toSignalProducer().start({ sender in
  self.validateLoginInput()
})

Where loginSignal is declared as var loginSignal: RACSignal?.

其中loginSignal声明为var loginSignal:RACSignal?。

This is in my validateLoginInput

这是在我的validateLoginInput中

self.loginStatus.value = MyStatus.Login.IN_PROGRESS // So button would be disabled

session.rac_dataWithRequest(request).map({ data, response in
  return MyResponse(data, response)
}).startWithNext({ response
  // Say MyResponse class would check the reponse if login is successful
  if response.isSuccessful() {
    self.loginStatus.value = MyStatus.Login.SUCCESS
  } else {
    self.loginStatus.value = MyStatus.Login.FAIL
  }
})

The view should disable the button at first, then re-enable it when the session finishes and when response.isSuccessful() is true.

视图应首先禁用该按钮,然后在会话结束时以及response.isSuccessful()为true时重新启用它。

Well, it works for now, but I am wondering if I am using MVVM with ReactCocoa 4 appropriately.

嗯,它现在有效,但我想知道我是否正在使用MVVM和ReactCocoa 4。

Also, I've been getting a "warning" that bothers me a bit. This appears like a second after the HTTP request gets is response.

而且,我得到了一个让我感到困扰的“警告”。这似乎是HTTP请求获得响应后的第二个。

2015-12-02 12:15:32.566 MyProject[460:48610] This application is
modifying the autolayout engine from a background thread, which can
lead to engine corruption and weird crashes.  This will cause an
exception in a future release.

Is it because I'm using v4.0.0-alpha.4 on Swift 2.1? This actually delays the re-enabling of my button.

是因为我在Swift 2.1上使用v4.0.0-alpha.4吗?这实际上会延迟重新启用我的按钮。

I'm getting confused with the examples in the web as most of them are in Objective-C and I think some function names changed, etc...

我对Web中的示例感到困惑,因为大多数都在Objective-C中,我认为一些函数名称已更改,等等...

Thanks a lot!

非常感谢!

1 个解决方案

#1


1  

Others can speak about the MVVM approach you're taking, but regarding the warning you're seeing: that's because you're using UIKit from a background thread. I imagine this comes from the self.setLoginButtonStatus call. Depending on what signals your binding to that property it's possible (and it's happening in this case) that the values emitted by its producer are not emitted on the main thread.

其他人可以谈论你正在采取的MVVM方法,但关于你所看到的警告:那是因为你在后台线程中使用UIKit。我想这来自self.setLoginButtonStatus调用。根据绑定到该属性的信号,可能(并且在这种情况下发生)它的生成器发出的值不会在主线程上发出。

To fix that, you can use observeOn, to forward values to the main thread:

要解决这个问题,您可以使用observeOn将值转发给主线程:

self.viewModel.loginStatus
    .producer
    .observeOn(UIScheduler())
    .startWithNext { status in
          self.setLoginButtonStatus(status)
    )

#1


1  

Others can speak about the MVVM approach you're taking, but regarding the warning you're seeing: that's because you're using UIKit from a background thread. I imagine this comes from the self.setLoginButtonStatus call. Depending on what signals your binding to that property it's possible (and it's happening in this case) that the values emitted by its producer are not emitted on the main thread.

其他人可以谈论你正在采取的MVVM方法,但关于你所看到的警告:那是因为你在后台线程中使用UIKit。我想这来自self.setLoginButtonStatus调用。根据绑定到该属性的信号,可能(并且在这种情况下发生)它的生成器发出的值不会在主线程上发出。

To fix that, you can use observeOn, to forward values to the main thread:

要解决这个问题,您可以使用observeOn将值转发给主线程:

self.viewModel.loginStatus
    .producer
    .observeOn(UIScheduler())
    .startWithNext { status in
          self.setLoginButtonStatus(status)
    )