RxSwift:管理应用程序中的对象更新 [英] RxSwift : manage object updates in the app

查看:54
本文介绍了RxSwift:管理应用程序中的对象更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常关心如何管理对象属性更改.

I have a big concern about how to manage object properties changes.

假设我有一个汽车"类,有一些属性,如名称"、日期"、价格"等

Imagine I have a "car" class, with some properties like "name", "date", "price" etc.

在我的视图A"中,我显示了我从 API 中检索到的所有汽车.在视图 B &C、我可以显示和编辑视图 A 中所选汽车的特定信息(假设它是汽车 AAA").也可以使用 API 调用刷新汽车对象以检索有关汽车的最新信息.

In my view "A", I'm displaying all cars I retrieve from a API. In a view B & C, I can display and edit specific information about the car selected in the view A (let's say it's "car AAA"). it's possible too, to refresh the car object with a API call to retrieve the last information about the car.

我希望能够更新视图 A,如果用户在另一个视图中对对象汽车执行某些操作.或者,如果用户刷新"汽车对象以确保拥有关于这辆车的最后数据.但是如果用户只是更改了汽车的名称,我不想为此刷新视图A中的所有UI..我想知道只是名称发生了变化.

I want to be able to update the view A, if the user does something on the object car in another view. Or, if the user "refreshed" the car object to be sure to have the last data about this car. But if the user just changed the name of the car, I don't want to refresh all the UI in the view A just for that.. I want to know that just the name has changed.

在视图 A 中捕捉这辆车的任何更新的最佳方法是什么?我是否必须有一个类将汽车的所有属性管理为PublishRelay"或PublishSubject",并在我的完整应用中使用它?

What is the best way to catch any update for this car in the view A? Do I have to have a class which manage all properties of the car as "PublishRelay" or "PublishSubject", and play with this in my full app?

推荐答案

我非常关心如何管理对象属性更改.

I have a big concern about how to manage object properties changes.

请记住,ReactiveX 是一个功能范例.对象的属性不会改变.而是从 Observable 发出一个具有不同属性的新对象.

Keep in mind that ReactiveX is a functional paradigm. The properties of objects don't change. Rather a new object is emitted from the Observable with different properties.

如果您只对跟踪特定汽车的名称感兴趣,那么这是一个简单的映射:

If you are only interested in tracking the name of a specific car then it's a simple mapping:

extension ObservableType where E == [Car] {
    func nameOfCar(withID id: Int) -> Observable<String?> {
        return map { $0.first(where: { $0.id == id }) }
            .map { $0?.name }
            .distinctUntilChanged()
    }
}

由于 distinctUntilChanged() 操作符的存在,上面的代码只会在输入 ID 的汽车名称发生变化时发出一个值.可选项在那里,因此消费者可以检测汽车是否不再在阵列中.从技术上讲,这个操作符观察一个发出汽车数组的 Stream,映射每个发出的数组以查看具有正确 ID 的汽车是否存在,如果存在,则查找该汽车的名称.然后它将该名称与它发出的先前名称进行比较,并仅在名称不同时才发出该名称.

Because of the distinctUntilChanged() operator, the above will only emit a value if the car with the entered ID's name changes. The optional is there so the consumer can detect if the car is no longer in the array. More technically, this operator observes a Stream that emits arrays of cars, maps over each array emitted to see if the car with the correct ID exists, and if it does, finds that car's name. Then it compares that name with the previous name it emitted and emits the name only if it is different.

您可以通过以下方式更广泛地观察 Car 对象:

You can observe Car objects more generally with this:

extension ObservableType where E == Car {
    var name: Observable<String> {
        return map { $0.name }
            .distinctUntilChanged()
    }

    var price: Observable<Double> {
        return map { $0.price }
            .distinctUntilChanged()
    }

    // etc...
}

extension ObservableType where E == [Car] {
    func car(withID id: Int) -> Observable<Car?> {
        return map { $0.first(where: { $0.id == id }) }
            .distinctUntilChanged() // assumes a Car is Equatable
    }
}

我希望可以使用 Swift 的新键路径系统做出更通用的解决方案.

I expect a more generic solution could be made with Swift's new key path system.

这篇关于RxSwift:管理应用程序中的对象更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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