Performance problems stemming from GeometryReader & ViewModel

Viewed 394

I'm having some serious performance problems.

Here is what I know:

  • The performance problems only started after I added a parent GeometryReader and a child Geometry Reader. I did this so I could read the current value of the ScrollView. Here is that concept, isolated:
  • The problem is not from GeometryReader by itself but the intersection method: enter image description here

I am calling that here:

 View()
.background(GeometryReader { child -> Color in
                                    let bounds = child.frame(in: .global)
                                    let frame = parent.frame(in: .global)
                                    
                                    if frame.intersection(bounds).size.width > frame.size.width * 0.55 {
                                        DispatchQueue.main.async {
                                            vm.selectedMovie = movie
                                        }
                                    }
                                    return Color.clear
                                })

The purpose of that method is to update a class property when the frame gets to a certain position.

Here is a demo of how it is used and performance:

demo

Relevant Code

GeometryReader { parent in <-- RootView 
  ...
    ZStack{...}.background(GeometryReader { child in 
       //This is where the intersection method I posted above is called

What can I do to improve performance?? (Let me know if I left out any important details)

0 Answers
Related