UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI: Optimizing refresh time for computed properties in structs

Forums > SwiftUI

Hi,

I have a main struct in my code, which contains about 50 stored properties and 50 computed properties that are function of the stored properties. I would say about 10 computed properties are rather complex calculations.

On my main screen, most of the computed properties can be shown if the user toggles different sections, but by default the majority are hidden. When I put a TextField that binds a value to one of the stored properties in question, on my iPad Pro 1st-gen, the refresh is very slow.

My assumption is, every time a stored property is updated, all the computed properties get calculated once again. Is that a correct premise? I would like them to be calculated only if they do appear in the view.

If so, is there any way to optimize things? So far, I tried :

  • Converting computed properties to lazy variables : This hasn't worked because the console warns me that Modifying state during view update, this will cause undefined behavior.
  • Converting computed variables to functions instead : This doesn't seem to have any effect on the refresh time. Can anyone confirm if this is a good solution or not?

Thanks a lot !

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.