4

I'm trying to dynamically build a view using SwiftUI for my widget. However, when multiple views are stacked, the widget no longer works. I want to loop my Widget over an array of fetched data, like so:

VStack {
    ForEach(entry.layers.indices) { index in
        let layer = entry.layers[index]
        Text(layer)
    }
}

but this doesnt work properly when the forEach loops more than about 5 times (it works fine when only looping once or twice!). I contacted Apple Code Level Support about this and they told me this is a 'bug'. See the example project I provided: https://github.com/swifty-on-me/Widget-Example

This is the data I'm trying to render: https://github.com/swifty-on-me/Widget-Example/blob/main/Data/FakeData.swift

I tried something like converting a view to image but the solution doesn't work in Extensions.

So I found this 'workaround' on the Apple Developer Forums but I'm unsure how to implement it: https://developer.apple.com/forums/thread/665935

Could someone explain or guide me to this workaround? (I'm not so at home with Swift(UI))

I've been messing around with this for ages now and I think I'm going insane about what seems like such a simple task.

See this example (about 28 seconds)

Kipnoedels
  • 1,118
  • 7
  • 22

2 Answers2

1

It looks like you are going against how SwiftUI works. You should not have code inside a view declaration. I'm surprised that code even compiled.

Here's where the problem appears to be:

GeometryReader { geo in
    ZStack(alignment: .topLeading) {
        Color.init("#bf1b49")
        VStack {
            ForEach(entry.layers.indices) { index in
                let layer = entry.layers[index] // < Here
                Text(layer)
            }
        }
    }
}

I'm not entirely sure what you are trying to do here, but you don't need two lines of code to do that. You could probably try moving the entry.layers[index] into the Text, because you don't need to store a variable for that. Like this:

GeometryReader { geo in
    ZStack(alignment: .topLeading) {
        Color.init("#bf1b49")
        VStack {
            ForEach(entry.layers.indices) { index in
                Text(entry.layers[index])
            }
        }
    }
}

Notice how the let layer is gone and the code that makes up layer is now within the text directly. Hopefully this helps!

Ben216k
  • 567
  • 4
  • 7
0

I too have the same problem. I try to show month view of a calendar in widget (with additional info from my app). I use ForEach and many VStack and HStack. The widget is very unreliable to load. I still try to figure out what causes this issue but I found out one thing… The widget does load successfully when I test it from around 2PM - midnight my local time. (Both when launching app from Xcode and from TestFlight) I know this is weird but it is what happens to me right now.

And yes removing some elements from widget makes widget loads successfully.

Thank you Boon

Boon
  • 111
  • 1
  • 5
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. – shim Aug 26 '21 at 21:01