-1

I have more than ten views in a struct, the compiler calls the issue, then I'm use group to fix the problem. if I group more the twenty views, it will cause a memory leak?

VStack {
            
            Text("Congratulations")
                .font(.largeTitle)
                .fontWeight(.bold)
            
            Spacer()
            
            // This grouping solved the problem
            Group {
                Text("You mastered a maze with 6 rooms!")
                Text("You found all the 3 hidden items")
            }
            
            Spacer()
            
            Text("Your Progress")
                .font(.largeTitle)
                .fontWeight(.bold)
            
            GameProgressView(gameProgress: gameProgress)
            
            Spacer()
            
            Text("You can now enter Level 4")
                .multilineTextAlignment(.center)
            
            Spacer()
            
            RHButton(title: "OK", action: { print("OK pressed") })
        }
fatdrogen
  • 463
  • 3
  • 10

1 Answers1

1

A memory leak is a specific problem when you have objects that can't be deallocated because you either lose a reference to them and can't delete them, or there is a reference cycle when two (or more) objects have strong references to the others so that they can't be released.

Adding more SwiftUI Views doesn't cause these problems, So you won't be getting memory leaks by just adding more groups, or having more than 20 views.

If you find it looks odd to have so many groups within your View, you can extract out some of the groups to their own Views instead. Again, this, in itself, will not cause a memory leak.

Abizern
  • 146,289
  • 39
  • 203
  • 257