使用 Realm 集合和 Combine 时在哪里调用 freeze()?
Where to call freeze() when working with Realm collections and Combine?
我想在只读视图 (SwiftUI) 中显示一些 Realm 对象,据我所知,应该为此类视图冻结 Realm 对象。
但是我应该在哪里调用.freeze()
?在 Results
-对象或发布者上?
Realm().objects(Contact.self)
.freeze()
.publisher
.[...]
对比
Realm().objects(Contact.self)
.publisher
.freeze()
.[...]
还是没有区别?
我刚刚测试了两种变体:
cancellable = try! Realm().objects(Contact.self)
.freeze()
.collectionPublisher
.map { ... }
.receive(on: DispatchQueue.main)
.assertNoFailure()
.assign(to: \.contacts, on: self)
对比
cancellable = try! Realm().objects(Contact.self)
.collectionPublisher
.freeze()
.map { ... }
.receive(on: DispatchQueue.main)
.assertNoFailure()
.assign(to: \.contacts, on: self)
虽然两个变体都在编译,但第一个变体不起作用:
由于未捕获的异常 'RLMException',正在终止应用程序,原因:'Frozen Realms do not change and do not have change notifications.'
因此必须在发布者上调用 freeze()
。
我想在只读视图 (SwiftUI) 中显示一些 Realm 对象,据我所知,应该为此类视图冻结 Realm 对象。
但是我应该在哪里调用.freeze()
?在 Results
-对象或发布者上?
Realm().objects(Contact.self)
.freeze()
.publisher
.[...]
对比
Realm().objects(Contact.self)
.publisher
.freeze()
.[...]
还是没有区别?
我刚刚测试了两种变体:
cancellable = try! Realm().objects(Contact.self)
.freeze()
.collectionPublisher
.map { ... }
.receive(on: DispatchQueue.main)
.assertNoFailure()
.assign(to: \.contacts, on: self)
对比
cancellable = try! Realm().objects(Contact.self)
.collectionPublisher
.freeze()
.map { ... }
.receive(on: DispatchQueue.main)
.assertNoFailure()
.assign(to: \.contacts, on: self)
虽然两个变体都在编译,但第一个变体不起作用: 由于未捕获的异常 'RLMException',正在终止应用程序,原因:'Frozen Realms do not change and do not have change notifications.'
因此必须在发布者上调用 freeze()
。