- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在过去的两年里,我使用 MVVM 模式构建应用程序,并且每个教程总是说 MVVM 使用 Reactive 库,例如:RXSwift 或 ReactiveCocoa,因为我是 iOS 程序员。我才发现
我只知道 Reactive 是函数式编程,意思是声明式编程。
最佳答案
对我来说,单独使用委托(delegate)似乎是实现应用程序逻辑的最长方法。响应式更简洁,需要更少的代码,是一种非常强大的做事方式。
举个例子,假设你想使用 RxSwift/RxCocoa 和 swift 4 创建一个 Reactive ButtonTap 系统:
1) 您创建一个协议(protocol),其实现方式与创建普通委托(delegate)抽象类时的方式相同。请注意,我们符合@objc
import RxSwift
import RxCocoa
// MARK: - ButtonTap Delegate protocol
@objc public protocol ButtonTapDelegate: NSObjectProtocol {
@objc func button(didSelect view: UIView, at index: Int)
}
2) DelegateProxyType 类是 react 性事件发生的地方。就像您从委托(delegate) (ButtonTapDelegate) 派生类一样,此类不仅会执行此操作,还会使用 PublishSubject 处理委托(delegate)消息。
// MARK: - ButtonTap DelegateProxy
open class ButtonTapDelegateProxy: DelegateProxy<UIView, ButtonTapDelegate>,
DelegateProxyType, ButtonTapDelegate {
/// Typed parent object.
public weak private(set) var buttonView: UIView?
internal var didSelectSubject = PublishSubject<(view: UIView, index: Int)>()
// MARK: - parent object for delegate proxy.
public init(parentObject: UIView) {
self.buttonView = parentObject
super.init(parentObject: parentObject, delegateProxy: ButtonTapDelegateProxy.self)
}
// MARK: - Register known implementationss. (from DelegateProxyType)
public static func registerKnownImplementations() {
self.register { ButtonTapDelegateProxy(parentObject: $0) }
}
// MARK: - read the current delegate. (from DelegateProxyType)
public class func currentDelegate(for object: UIView) -> ButtonTapDelegate? {
return object.delegate
}
// MARK: - set the current delegate. (from DelegateProxyType)
public class func setCurrentDelegate(_ delegate: ButtonTapDelegate?, to object: UIView) {
object.delegate = delegate as? ButtonTapDelegateProxy
}
// MARK: delegate method
public func button(didSelect view: UIView, at index: Int) {
didSelectSubject.onNext((view, index))
}
// MARK: - dispose the publish subject
deinit {
didSelectSubject.on(.completed)
}
}
3) 然后,只需创建您具有委托(delegate)属性的自定义按钮类。您可以通过调用其抽象方法将消息简单地传递给您的委托(delegate)。
// MARK: - create Custom ButtonView class with the delegate property
open class ButtonView: UIView {
@IBOutlet weak open var delegate: ButtonTapDelegate?
func tapButtonAction() {
let view = UIView()
let index = 2
delegate.button(didSelect: view, at: index)
}
}
// MARK: - ButtonView must have delegate property
extension ButtonView: HasDelegate {
public typealias Delegate = ButtonTapDelegate
}
4) 您可以使用 RxCocoa 捕获您的委托(delegate)发送的消息。您只需要使用 View 的这个扩展 (ButtonView) 从您的 DelegateProxy 类返回到您的 PublishSubject。请注意,该扩展程序是响应式的
// MARK: - Custom ButtonView with the Reactive delegate and its protocol function
public extension Reactive where Base: ButtonView {
/// Reactive wrapper for `delegate`.
/// For more information take a look at `DelegateProxyType` protocol documentation.
fileprivate var delegate: ButtonTapDelegateProxy {
return ButtonTapDelegateProxy.proxy(for: base)
}
public func setDelegate(_ delegate: ButtonTapDelegate) -> Disposable {
return ButtonTapDelegateProxy
.installForwardDelegate(delegate, retainDelegate: false, onProxyForObject: self.base)
}
public var didSelect: ControlEvent<(view: UIView, index: Int)> {
return ControlEvent(events: delegate.didSelectSubject)
}
}
5) 在您的 ViewController 中,您将使用 RxSwift 监听任何按钮点击并捕获由 PublishSubject 在您的 DelegateProxy 类中发送的那些事件。这与 RxSwift 手势示例没有什么不同:https://github.com/RxSwiftCommunity/RxGesture
class myViewController: UIViewController {
@IBOutlet weak var buttonView: ButtonView!
override func viewDidLoad() {
super.viewDidLoad
buttonView.rx.didSelect.asObservable()
.subscribe(onNext: { view, index in
// button tapped in view at index
}).disposed(by: bag)
}
}
这个过程与所有 RxSwift 和 RxCocoa Reactive delegates 的工作方式非常相似,它们已经在许多 UIKit 元素上实现了,如下所示:https://github.com/ReactiveX/RxSwift/tree/master/RxCocoa/iOS
响应式非常灵活和强大,不需要一直调用委托(delegate)方法和设置委托(delegate)。这只会发生一次,想象一下如何在 viewController 中处理 CustomViews 的不同组合。
关于ios - 有什么代表比 Reactive 做不到的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45955694/
类型‘AbstractControl’上不存在属性‘Controls’。
主要是我很好奇。 我们有一个名为 Unit 的对象在我们的代码库中 - 代表桥梁或道路的组件。在我们的例子中,看到带有 Unit 的 ReactiveUI 命令可能会模棱两可。作为声明中的泛型之一。
我一直听说六边形架构必须与任何框架无关,并使用接口(interface) (SPI) 来委托(delegate)不属于业务层的每个代码部分。 但是如何在不使用额外框架的情况下通过六边形架构创建一个响应
我读了 Reactive Manifesto . 但我无法理解 event driven architectures 之间的核心差异和 message driven architectures .结果
申请要求: 订阅两个事件流 A 和 B 对于每个 A 事件,一段时间后应该有相应的 B 事件 如果没有相应的 B 到达(及时),应用程序会监视 A 事件并发出警报 B 事件可以以与 A 事件不同的顺序
Closed. This question is opinion-based。它当前不接受答案。 想改善这个问题吗?更新问题,以便editing this post用事实和引用来回答。 4年前关闭。
我有一个 ViewModel,它在其初始化程序中有一个输入 init(sliderEvents: Reactive) { 在测试中我想做类似的事情 slider.send(.touchDownInsi
经典实时搜索示例: var searchResults = from input in textBoxChanged from results in GetDa
我有一个响应式(Reactive)管道来处理传入的请求。对于每个请求,我需要调用一个与业务相关的函数 ( doSomeRelevantProcessing )。 完成后,我需要将发生的事情通知一些外部
是否可以为响应式扩展实现基于硬件计时器的自定义调度程序?我该如何开始,有什么好的例子吗? 我有一个硬件可以每毫秒向我发送一个准确的中断。我想利用它来创建更精确的 RX 调度程序。 更新 感谢 Asti
我正在通过网络浏览 Rx 框架 Material ,我发现了很多。 现在,每当我为此在 google 上搜索时,我还会在 wikipedia 链接中找到“响应式(Reactive)编程”。 由于响应式
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 6年前关闭。 Improve this
SignalR 与响应式扩展是同一回事吗?你能解释一下为什么或为什么不吗? 最佳答案 不,它们绝对不是同一件事。 Reactive Extensions 是一个用于创建和组合可观察的数据流或事件流的库
我知道有一种简单的方法可以做到这一点 - 但今晚它打败了我...... 我想知道两个事件是否在 300 毫秒内发生,就像双击一样。 在 300 毫秒内单击两次左键鼠标 - 我知道这是构建响应式(Rea
我们的应用程序使用 Reactive Extensions (Rx)。这些通常通过 Microsoft 的可下载包安装。但是,当我们发布应用程序时,我们会提供 dll 的副本(即 System.Cor
我想了解 Reactive 和 ReactiveStreams 之间的区别,特别是在 RxJava 的上下文中? 我能想到的最多的是 Reactive Streams 在规范中有一些背压的概念,但它已
我想探索来自 Quarkus 的响应式 REST 客户端的慢速后端,并在他们建议的样本 (https://github.com/quarkusio/quarkus-quickstarts/tree/m
假设我有一个存储桶,我需要从中获取日期早于现在的文档。 该文档如下所示: { id: "1", date: "Some date", otherObjectKEY: "key1" } 对于每个文档,我
我有一个 RIA 服务数据服务,它有几个函数调用,如下所示: public InvokeOperation SomeFunc( SomeData data, Action> callb
我一直在使用 Rx 在单个应用程序中创建事件总线(想想 CQRS/ES),它似乎工作得很好。然而,在调查了一堆不同的事件溯源框架之后,我还没有看到使用过一次 Rx。与基于反射/容器的调度程序相比,它似
我是一名优秀的程序员,十分优秀!