I have a view that I want to disable a view wile the network is busy. I have the following observable object that has state depending on the what is going on with the network. This is in SwiftUI 2.0 and Mac BigSur.
class FeedModel: ObservableObject {
enum CurrenState: Equatable {
case none
case loading
case error
case done
}
private (set) var feed: RssFeed?
@Published private (set) var currentState: CurrenState = .none
... network code.
}
My view has the following code:
struct FeedAddressSheet: View {
@State var text: String = ""
@Binding var isOpen: Bool
@ObservedObject var model = FeedModel()
public var body: some View {
ZStack {
VStack(alignment:.leading) {
Text("Enter a feed address")
TextField("", text: $text)
.frame(idealWidth: 300)
HStack {
Spacer()
Button("Cancel") {
isOpen = false
}
.keyboardShortcut(.cancelAction)
Button("Add") {
model.load(address: text)
}
.keyboardShortcut(.defaultAction)
}
}
.padding()
.disabled(model.currentState == .loading)
if model.currentState == .loading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
}
}
.alert(isPresented: Binding.constant(model.currentState == .error)) {
Alert(title: Text("error"))
}
}
}
The line where I have .disabled(model.currentState == .loading)
is causing the following error.
=== AttributeGraph: cycle detected through attribute 247128 ===
=== AttributeGraph: cycle detected through attribute 242488 ===
=== AttributeGraph: cycle detected through attribute 249240 ===
=== AttributeGraph: cycle detected through attribute 249240 ===
=== AttributeGraph: cycle detected through attribute 250280 ===
=== AttributeGraph: cycle detected through attribute 250280 ===
=== AttributeGraph: cycle detected through attribute 242488 ===
=== AttributeGraph: cycle detected through attribute 252824 ===
I am not sure why I am getting a cycle.
So this appears to be an issue with the text field and being bound to a state var.. The following code is shows the issue.
```struct ContentView: View {
@State var enabled = true
@State var text = ""
var body: some View {
VStack {
TextField("", text: $text)
.padding()
.disabled(!enabled)
Button("Press me") {
enabled.toggle()
}
}
}
}