See below playground code:
import SwiftUI
import PlaygroundSupport
struct InnerView: View {
@Binding var text: String
@State private var startedChange = false
var body: some View {
Text("Hello").onAppear {
if !self.startedChange {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.text = "cat"
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.text = "cheetah"
}
self.startedChange = true
}
}
}
}
struct ContentView: View {
@State private var animalName = "dog"
var body: some View {
VStack {
InnerView(text: $animalName)
Text(animalName)
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
The bottom Text(animal.name)
alternates irregularly between "cat" and "cheetah," in an infinite loop of race conditions. I expected the text to change to "cat" after one second and then "cheetah" after another.
I would not have expected
@State private var startedChange = false
to be initialized more than once.
Where is my mental model wrong?
(Xcode 12.0)
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi all!When presenting a specific modal sheet in a SwiftUI project, I consistently get a ton of console output like:=== AttributeGraph: cycle detected through attribute 290 ===Like, a couple hundred of these logs. It appears to be accompanied by a stutter/slowdown in the UI, but there's no crash and everything looks to be in the right place.Is the "cycle" here some sort of ARC retain cycle? Is it SwiftUI-specific? (I'm asking here because I've never seen this warning in a classic UIKit project.) Where can I go to learn more about this?Thanks!