0

I have multiple UIStackViews in a scroll view. As the view is loaded each stack view, say 6, is populated with ~8 views in each. This means at a table view cell tap, 48 views will be initialized and equally added to one of 6 stack views. As you can imagine there is a 0.5 - 1 second lag when tapping the table view cell to load the stack view. It definitely feels like a slow down and doesn't feel right in an iOS app.

Simplified my code looks like this:

for (index, stackView) in stackViews.enumerated() {
  for view in views[index] {
    stackView.addArrangedSubview(view)
  }
}

I have considered these options:

  • Adding/removing the stack views when they come in and out of view, although on an iPad all stack views can be seen anyway.
  • Multi-threading, although I am not sure if this is possible as all UI code needs to be ran on the main thread.
  • Simply using draw as the views within the stack view are all mostly similar in appearance.

Are any of these worth committing to? Are there any other options I have not considered?

mac38478
  • 169
  • 5
  • 1
    Why not choose CollectionView? – Tieda Wei Dec 03 '19 at 20:49
  • Can you show a bit more of what you are doing? I just did a quick test, adding 8 `UILabel`s to each of 6 `UIStackView`s (so, total of 48 labels), and they appear pretty much instantly... `0.0299...` seconds. – DonMag Dec 03 '19 at 20:55
  • @DonMag There are other container views that the stack view sits inside in order to style it (e.g. rounded corners, drop shadow). Your results make me think these may been causing slow downs as well. For debugging sake, what tool are you using to determine time between interactions? – mac38478 Dec 03 '19 at 21:13
  • 1
    @mac38478 - yes, that will definitely be an issue. Comparing `CFAbsoluteTimeGetCurrent()` from before and after the loop, I get under 0.03 seconds... Using Instruments -> Time Profiler, with the loop in a func of its own (to try and get it as close as possible), I'm seeing 16.00 ms. – DonMag Dec 03 '19 at 21:40
  • @mac38478 - here is sample code for my quick test: https://pastebin.com/TNAjWc3L – DonMag Dec 03 '19 at 21:46

1 Answers1

1

Go with option (2). As mentioned here, preparing views off the main thread can be fine. If those views are similar enough, I'd definitely prepare them beforehand and have a pool somewhere. Further, check the any data processing impact: are you displaying any images or anything else? (this can not be seen in the given code, provide more if you want).

A. Gldmn
  • 11
  • 2
  • I'm only changing a label value and the background color of the view, even adding dozens of just plain UIViews causes a slowdown. After testing it looks like I can only initialize a UIView from the main thread so it seems that multi-threading might be out of the question. – mac38478 Dec 03 '19 at 20:03