gpt4 book ai didi

ios - 带有 Realm 对象存储的 RxSwift 变量

转载 作者:行者123 更新时间:2023-11-28 12:52:28 24 4
gpt4 key购买 nike

我在理解是否支持将 RxSwift 与 Realm 结合使用时遇到了一些问题,因为 Realm 明确说明了支持的变量类型。

是否可以代替以下内容。

import RealmSwift

class Dog: Object {
dynamic var name = ""
}

做这样的事情。

import RealmSwift
import RxSwift

class Dog: Object {
var name = Variable<String>("")
}

我进行了一些谷歌搜索,但没有找到任何关于扩展或其他解决方案的最新信息。将不胜感激任何帮助或只是指向正确的方向。

最佳答案

使用 KVO rx_observe() 怎么样?像下面这样:

dog
.rx_observe(String.self, "name")
.subscribeNext { name in
print("string: \(name)")
}

但是对于没有使用 KVO 持久化的 Realm 对象有一个限制。

对于持久化对象,不限制观察。

注意:

Observing properties of standalone instances of Object subclasses works just like with any other dynamic property, but note that you cannot add an object to a Realm (with realm.add(obj) or other similar methods) while it has any registered observers.

https://realm.io/docs/swift/latest/#key-value-observation

关于ios - 带有 Realm 对象存储的 RxSwift 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com