3

What is the maximum number of state variables I can have in a Swift UI Struct ?

struct Example: View {

    @State var first : Bool = true
    @State var second: Double = 94.4
    @State var third: CGFloat = 45.45
        .
        .
        .
     How many maximum ?

    var body: some View {
        Text("Hello ")
    }

}

Also, does having a lot of state variables actually slow down the app ? I need some clarity on the scalability of this thing cuz I am new to declarative programming :P

Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52

3 Answers3

4

Just to get you started with an answer,

  1. It's hard to believe there is any performance issue. "Declarative programming" doesn't actually exist. It's just a compiler / runtime / whatever doing some checking. There's no really substantative, paradigm difference from other structures in the pipeline.

If, incredibly, you were doing real performance programming, perhaps scientific or for a game or the like, you'd never in a million years be involved in anything like this, so it's of no relevance.

Also, it's worth nothing that anything at all related to UI on your phone, uses a staggering amount of processing power. Rendering any one text character! which happens to be on screen at that time, is an amazing dance of spline curve dithering, blitting, etc. Issue such as "checking done by the runtime system" are really irrelevant except in incredibly unusual cases.

Summary: don't consider performance in this milieu.

  1. How many can you have? If you paste in a few thousand, it works fine. It's hard to believe there's any limit.

Summary: It's possible you're thinking of a limit like "30". There is no such limit whatsoever.

(There could be some technical, arcane limit - like 64 million - but it's totally irrelevant to what you're doing!)


Management summary: relax and enjoy, these are non-issues.


More info on the particular case

I am trying to animate, i am using withAnimation to display percentage progress in a ring

For the number of state variables described (say, anything less than 100,000 - you are using about "20"), the extra overhead for the checking is utterly irrelevant. It is so low you could not measure it.

TBC this is likely not the most elegant way to do this but, regarding the specific performance question asked, it is absolutely a non-issue. Enjoy!

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • 1
    the fact that structs in swift are immutable so each time you change a value it destroys and rebuilds itself concerns me. So what do yo have to say for that ? – Tilak Madichetti Mar 07 '20 at 16:28
  • hi @TilakMaddy ! It's a fine question but honestly, the performance consideration is non-existent. Let me ask it this way - how often (say, per minute) are you expecting that value to change ? let us know! – Fattie Mar 07 '20 at 17:06
  • 60 times per minute apparently - but that is for all my 20 state variables combined ! – Tilak Madichetti Mar 08 '20 at 03:54
  • @TilakMaddy , there is something strange if you are doing it at frame rate. (A) you're trying to animate or? if so, use the various animation system in iOS, use CADisplayLink, or (in extreme cases) a lower level solution. However, (B) 20 times a frame is ........ utterly nothing :) the processor can handle that. you would not even be able to measure it. cheers! – Fattie Mar 08 '20 at 15:17
  • i am trying to animate but i am using `withAnimation` to display percentage progress in a ring that i get from firebase storage upload task listener's` on progress `method – Tilak Madichetti Mar 09 '20 at 16:49
  • ok, FACT, it is utterly no problem performance wise. Honestly, you are out by a factor of, let me think about *ten million* :) You're good to go! – Fattie Mar 09 '20 at 17:18
  • thanks for your help @Fattie, but can i get your contact if possible ! i am 18 just getting started into iOS [3 months in] (used to do web dev before). Am not gonna bother but like in case i really lack resource on any topic or need some kind of guidance i want to ping you for help. Thanks !!! – Tilak Madichetti Mar 09 '20 at 18:05
  • 1
    hi @TilakMaddy ! If you post a question on this site, you are more than welcome to @tag me (like you just did) and I will answer, as well as everyone else! the best programmers start young, enjoy! I encourage you to master `CALayer` (which is really at the heart of animation), best! https://stackoverflow.com/a/57400842/294884 – Fattie Mar 09 '20 at 19:32
1

Not sure if this is a bug but I've reached the limits of SwiftUI with an app that has 85 @State variables on 1 screen. Also I have a lot of stacked groups inside groups inside groups. I'm at the stage where if I add any 1 line of code, the compiler will complains that what I'm writing is not a View.

So there is a limit depending on how fast your computer can compile the View. The compiler has a set amount of time it is allowed to try and compile until it just stops abruptly.

TimBigDev
  • 511
  • 8
  • 7
0

I got the same problem, when I try to put one more state, it does not compile:

(372, 10) The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

but when remove any state and use the new one it works. So definitely it has the limits!!! Another question is how to solve it.

Nurseyit Tursunkulov
  • 8,012
  • 12
  • 44
  • 78