views:

271

answers:

2

I am trying to speed my gui that loads very slow slow when I am loading a large project (the gui is a representation of groups and sub groups and is made up of many views). During this process I was looking at how long certain code segments take to execute and I have found that a call to addsubview is taking between 10 and 20 milliseconds most of the time. The subview I was looking at is a disclosure button. I am wondering if this method is just inherently slow or is their some other factor at work here? Is the time it takes to add the subview dependent on the complexity of the subview or is that not a factor? Also, is there some other method that can be used to add a subview that might be faster?

+1  A: 

Rearranging the view hierarchy isn't something you'd typically do very often. Instead of adding and removing subviews, you could hide and unhide them.

NSResponder
I am not actually removing and adding subviews I hide them and then unhide them like you describe. The slow down happens when I add multiple subviews when I am loading up my GUI initially. The gui works fine when you are only adding one item at a time.
Mike2012
+1  A: 

You could try -setSubviews: which takes an array of subviews. This may be faster then calling -addSubview: multiple times yourself.

Otherwise, -addSubview: and -addSubview:positioned:relativeTo: are the only other methods for inserting subviews.

I'm curious, though, why is 10 - 20 ms to slow for a single subview? How many subviews are you trying to add?

It is possible there is an alternative design using NSCell's that may be faster, but without know more details about what you are trying to accomplish, it is difficult to know.

ericgorr
My GUI is made up of a hierarchy of views, it is basically trying to mimic the functionality of the itunes GUI interface. I have groups that contain a disclosure button, an image and a text field (I add the image and text field, and button with calls to addsbubview) and then each group has a subgroup which has an image and a text field but no button. Also, each group and item is contained in a view which helps detect when the group or item is selected. My problem occurs when I initially load a project which could be comprised of many groups each with many items.
Mike2012
I believe you will need to rethink your implementation. Using more then several hundred individual views isn't going to work well. It will be slow.I would suggest the following:(1) Picking up a copy of "Cocoa Programming for Mac OS X (Third Edition)" by Aaron Hillegass and reading the section "For the More Curious: Cells" in the chapter on Custom Views. This will give you a brief explanation on the motivation behind cells.(2) Pick up a copy of "Cocoa Design Patterns" by Erik Buck and Donald Yacktman and read the Flyweight chapter. I would also suggest reading the rest of the book too.
ericgorr