-1

Here's my code

struct Home: View {
@State var status = UserDefaults.standard.value(forKey: "status") as? Bool ?? false
var userEmail = Auth.auth().currentUser?.email
@State private var hasTimeElapsed = false

var body: some View {
        if self.status {
            while (idkisrun == true) {
                if group_array.contains(userEmail ?? "1") {
                        HomeScreen()
                    } else {
                        Verify()
                    }
                    }
                    } else {
            VStack{
                Login()
            }

Here's the error

    error: closure containing control flow statement cannot be used with result builder 'ViewBuilder'
                while (idkisrun == false) {
                ^
SwiftUI.ViewBuilder:2:30: note: struct 'ViewBuilder' declared here
@resultBuilder public struct ViewBuilder {

How do I fix this to be able to wait for when the variable is true, and then continue the code from there?

Canyon
  • 45
  • 6
  • What causes `idkisrun` to change value? (And what declares it?) – Phillip Mills Dec 24 '21 at 01:06
  • This is the wrong way to approach SwiftUI. Instead of waiting for `idkisrun` to change it's value, you write what your `View` should look like if `idkisrun == true` and then what it should look like if `idkisrun == false`. Maybe using multiple `View` structs, maybe with a conditional inside a single `View`. – Patrick Wynne Dec 24 '21 at 01:25
  • You want to use `onChange(of:perform:)` to detect changes. Answer [here](https://stackoverflow.com/a/63289866/9607863) – George Dec 24 '21 at 01:40
  • PhillipMills, idkisrun is a public variable, and it's value changes from true to false inside a function being run above. Patrick Whynne, I've tried that, but the problem is, my function sets the value of the "group_array" function. idkisrun is just a way to see if the function is finished. I need the array to be updated before I continue. George, I don't think I understand what you mean. – Canyon Dec 24 '21 at 04:55
  • 1
    Please read a book or a tutorial about the ***declaritive*** design of SwftUI. Never ever use a `while` loop inside the rendering area of a view. There are APIs to observe changes, this means your function **tells** the environment when it has finished. And `value(forKey:` to retrieve a `Bool` from `UserDefaults` is wrong. Basically there is `bool(forKey:` but this is also discouraged in SwiftUI. Use the [`@AppStorage`](https://developer.apple.com/documentation/swiftui/appstorage) property wrapper. – vadian Dec 24 '21 at 05:39

1 Answers1

0

Try to make "idkisrun" as a Published variable (if it isn't). Than try to change (while) operator to (if). It should works. Also, "idkisrun" should be changed only in main tread to update a View. Hope it will helps, Good Luck!)

  • I already know it works with "if". The problem I have is the function setting "idkisrun" to true doesn't finish running until after the "if" statement has been run. I need that part of my code to "wait" until the function has finished running – Canyon Dec 24 '21 at 14:41
  • Trere is a thing, which can help - mvvm architecture. Try to update "idkisrun" in the function, that you are waiting for response – Матвей Марюха Dec 24 '21 at 16:08