RxSwift主题以更新UI

huangapple go评论79阅读模式
英文:

RxSwift Subjects to update UI

问题

I have an app that uses the delegate method to communicate controllers with ViewModels. But I would like to start using reactive programming with RxSwift.

我有一个应用程序,它使用委托方法来在控制器和ViewModel之间进行通信。但是我想开始使用RxSwift进行响应式编程。

The problem I have is that I don't know what subject to use.

我遇到的问题是我不知道要使用哪种主题(Subject)。

The first thing I need is an observable that can notify me when an array of data has changed, but that allows me to add values, modify values, and remove values. And that it can be bound to a tableView.

我需要的第一件事是一个可观察对象,它可以在数据数组发生变化时通知我,但同时允许我添加值、修改值和删除值。并且它可以绑定到一个tableView。

Is it okay to use BehaviorRelay?

使用BehaviorRelay可以吗?

public var array = BehaviorRelay<[Struct]>(value: [])

示例代码:

public var array = BehaviorRelay<[Struct]>(value: [])

Example with delegate:

使用委托的示例:

var posts: [PostStruct] {
    didSet {
        //Call delegate: didUpdatePosts(posts) -> Update TableView
    }
}

也可以执行以下操作:

posts.append(newPost)
posts.remove(at: index)
posts[index].name = "Hi"

and in the second occasion where I need to use RxSwift is to notify me of changes that have occurred in a property so that I can update the UI.

在我需要使用RxSwift的第二种情况下,我需要通知我属性发生了变化,以便我可以更新UI。

Example with delegate:

使用委托的示例:

var post: PostStruct {
    didSet {
        //Update UI
        //Call delegate: didUpdatePost(post) -> Update UI
    }
}

The posts are obtained from the database, so it is important to update the table view every time elements are added or deleted.

这些帖子是从数据库中获取的,因此在添加或删除元素时更新表视图非常重要。

So... basically what I'm looking for is an observable that notifies me when its value is changed, removed, or added, just like @Published on SwiftUI.

所以...基本上我正在寻找的是一个可观察对象,它可以在其值发生更改、删除或添加时通知我,就像SwiftUI中的@Published一样。

英文:

I have an app that uses the delegate method to communicate controllers with ViewModels. But I would like to start using reactive programming with RxSwift.

The problem I have is that I don't know what subject to use.

The first thing I need is an observable that can notify me when an array of data has changed, but that allows me to add values, modify values, and remove values. And that it can be binded to a tableView.

Is it okay to use BehaviorRelay?

public var array = BehaviorRelay&lt;[Struct]&gt;(value: [])

Example with delegate:

     var posts: [PostStruct] {
            didSet {
    //Call delegate: didUpdatePosts(posts) -&gt; Update TableView
            }
        }

//Also to allow:
posts.append(newPost)
posts.remove(at: index)
posts[index].name = &quot;Hi&quot;

and in the second occasion where I need to use RxSwift is to notify me of changes that have occurred in a property so that I can update the UI.

Example with delegate:

 var post: PostStruct {
        didSet {
//Update UI
//Call delegate: didUpdatePost(post) -&gt; Update UI
        }
    }

The posts are obtained from the database, so it is important to update the table view every time elements are added or deleted.

So... basically what I'm looking for is an observable that notifies me when its value is changed, removed, or added, just like @Published on SwiftUI.

Thanks

答案1

得分: 0

从书籍《Introduction to Rx》中提取的内容:

  1. "Subjects provide a convenient way to poke around Rx, however they are not recommended for day to day use."

    • "Subjects 提供了一种方便的方式来探索 Rx,但不建议日常使用。"
  2. "The essence of functional reactive programming is to specify the dynamic behavior of a value completely at the time of declaration."

    • "函数式响应式编程的本质是在声明时完全指定值的动态行为。"
  3. "let posts: Observable<[PostStruct]>"

    • "让 posts: Observable<[PostStruct]>"
  4. "It's clear to me that you need a state machine which means using the scan operator."

    • "我清楚你需要一个状态机,这意味着使用 scan 运算符。"
  5. "You mention data coming from a delegate which means you have to convert the delegate to Observables. This article 'Convert a Swift Delegate to RxSwift Observables' explains how to do that."

    • "你提到数据来自委托,这意味着你需要将委托转换为 Observables。这篇文章 'Convert a Swift Delegate to RxSwift Observables' 解释了如何做到这一点。"
  6. "The only time you should reach for a subject is when you are converting imperative code to declarative..."

    • "唯一需要使用 Subject 的时机是当你将命令式代码转换为声明式代码时..."
  7. "Avoid the use of the subject types. Rx is effectively a functional programming paradigm..."

    • "避免使用 Subject 类型。Rx 有效地是一种函数式编程范式..."

注意:已翻译的文本部分已经提供,不再回答关于翻译的问题。

英文:

From the book "Introduction to Rx":
> Subjects provide a convenient way to poke around Rx, however they are not recommended for day to day use.

Another useful quote:
> The essence of functional reactive programming is to specify the dynamic behavior of a value completely at the time of declaration.
-- Heinrich Apfelmus

So you want to declare:

let posts: Observable&lt;[PostStruct]&gt;

In order to specify the dynamic behavior of this value, we first need to know what causes this value to change? It doesn't change for arbitrary reasons, but you didn't really go into detail.

However, given what you did say:

  • It's clear to me that you need a state machine which means using the scan operator.
  • You mention data coming from a delegate which means you have to convert the delegate to Observables. This article "Convert a Swift Delegate to RxSwift Observables" explains how to do that.

The only time you should reach for a subject is when you are converting imperative code to declarative (which you should do as soon as possible, the RxCocoa library has done most of that for you already,) or when creating a feedback loop (which is often not needed.)

For more information:
> Avoid the use of the subject types. Rx is effectively a functional programming paradigm. Using subjects means we are now managing state, which is potentially mutating. Dealing with both mutating state and asynchronous programming at the same time is very hard to get right. Furthermore, many of the operators (extension methods) have been carefully written to ensure that correct and consistent lifetime of subscriptions and sequences is maintained; when you introduce subjects, you can break this.

huangapple
  • 本文由 发表于 2023年5月21日 09:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定