SwiftUI ObservableObject 用作 EnvironmentObject 奇怪的行为
SwiftUI ObservableObject used as EnvironmentObject odd behaviour
我有这样的 ObservableObject
,我正在使用 environmentObject()
.
注入视图层次结构
class MenuModel: ObservableObject {
@Published var selection: Int = 0
@Published var isMenuOpen: Bool = false
@Published var tabItems : [TabItem] = [TabItem]()
// {
// didSet {
// objectWillChange.send()
// }
// }
@Published var menuItems : [MenuItem] = [MenuItem]()
// {
// didSet {
// objectWillChange.send()
// }
// }
//var objectWillChange = PassthroughSubject<Void, Never>()
}
以下是我不太了解的问题:
1. 上面的代码可以正常工作,因为所有属性都是@Published。
2. 但是如果我把它改成这样
class Click5MenuModel: ObservableObject {
@Published var selection: Int = 0
@Published var isMenuOpen: Bool = false
var tabItems : [TabItem] = [TabItem]()
{
didSet {
objectWillChange.send()
}
}
var menuItems : [MenuItem] = [MenuItem]()
{
didSet {
objectWillChange.send()
}
}
var objectWillChange = PassthroughSubject<Void, Never>()
}
然后@Published 属性停止刷新依赖于此 ObservableObject 的视图!
这是为什么。我还尝试添加 didSet
和 objectWillChange.send()
但这也会导致一些奇怪的行为并且代码有点尴尬。
这是否意味着我只能使用 ONLY @Published 或 ONLY objectWillChange 方法?
默认实现就可以工作(只要你有@Published 属性)。这是来自 API 声明:
/// By default an `ObservableObject` will synthesize an `objectWillChange`
/// publisher that emits before any of its `@Published` properties changes:
...
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension ObservableObject where Self.ObjectWillChangePublisher == ObservableObjectPublisher {
/// A publisher that emits before the object has changed.
public var objectWillChange: ObservableObjectPublisher { get }
}
所以删除以下内容:
var objectWillChange = PassthroughSubject<Void, Never>()
并使用
didSet {
self.objectWillChange.send()
}
我有这样的 ObservableObject
,我正在使用 environmentObject()
.
class MenuModel: ObservableObject {
@Published var selection: Int = 0
@Published var isMenuOpen: Bool = false
@Published var tabItems : [TabItem] = [TabItem]()
// {
// didSet {
// objectWillChange.send()
// }
// }
@Published var menuItems : [MenuItem] = [MenuItem]()
// {
// didSet {
// objectWillChange.send()
// }
// }
//var objectWillChange = PassthroughSubject<Void, Never>()
}
以下是我不太了解的问题: 1. 上面的代码可以正常工作,因为所有属性都是@Published。 2. 但是如果我把它改成这样
class Click5MenuModel: ObservableObject {
@Published var selection: Int = 0
@Published var isMenuOpen: Bool = false
var tabItems : [TabItem] = [TabItem]()
{
didSet {
objectWillChange.send()
}
}
var menuItems : [MenuItem] = [MenuItem]()
{
didSet {
objectWillChange.send()
}
}
var objectWillChange = PassthroughSubject<Void, Never>()
}
然后@Published 属性停止刷新依赖于此 ObservableObject 的视图!
这是为什么。我还尝试添加 didSet
和 objectWillChange.send()
但这也会导致一些奇怪的行为并且代码有点尴尬。
这是否意味着我只能使用 ONLY @Published 或 ONLY objectWillChange 方法?
默认实现就可以工作(只要你有@Published 属性)。这是来自 API 声明:
/// By default an `ObservableObject` will synthesize an `objectWillChange` /// publisher that emits before any of its `@Published` properties changes: ... @available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) extension ObservableObject where Self.ObjectWillChangePublisher == ObservableObjectPublisher { /// A publisher that emits before the object has changed. public var objectWillChange: ObservableObjectPublisher { get } }
所以删除以下内容:
var objectWillChange = PassthroughSubject<Void, Never>()
并使用
didSet {
self.objectWillChange.send()
}