Performance difference: loadNibNamed and software

I have a viewcontroller that contains many custom UIViews. The custom UIView that I tried to determine using InterfaceBuilder (IB) and load it into initWithFrame using this code:

NSArray *xib=[[NSBundle mainBundle] loadNibNamed:@"DayView" owner:self options:nil]; 

The view controller was extremely slow, so I decided to try loading the user interface elements programmatically. Vòila, and the speed was increased by about 7 times.

Why is the difference between loading XIB files and using clean code so big? My first hypothesis that I could think of was that IB sets a lot of default properties, whereas they are simply null when defining them in code. But that cannot explain the huge difference in performance! I did not find a single post that would explicitly warn you against using IB for performance reasons.

EDIT: I just found this link and therefore this blog which explains interesting things about when XIB files are loaded from disk. I think booting from the disk explains the difference. / K

+6
source share
1 answer

Here are some points that I got, and also found in my experience and Google.

 => I've done it both ways and here my two sense: 

If your view is relatively simple, and you are sure that he will not change it much; then xib away. If not, I recommended everything programmatically, because it has at least 3 great advantages:

1) You get a much deeper knowledge of how everything works behind the scenes, which greatly facilitates debugging if there are problems.

2) Full control and configuration throughout, so if there are changes, they are easy to implement

3) Less entanglement; this may be a personal preference, but if a lot of actions are called from the buttons, and I find that the interface builder arrow zig-zag hullabaloo is much more confusing than programmatically.

=> programmatically define control is a more time-consuming process, and then use XIB. Because to create all controls and positions, etc. On the fly.

=> I also noticed that files related to loading a view programmatically use about half of the secondary memory, such as xib files. I'm not sure if this translates into a larger application, but it might be something to consider in lager applications

=> I read something, and I think I found something that prints a deal in favor of software. It turns out that Xib files implicitly use the imageNamed method to load their images into memory, which has a well-known caching function that is memory problematic. I also heard that IBOutlets is a significant memory management issue.

All these front-end builders are very good tools, but if you don’t know what you are doing and you don’t know what these tools do, you

1. Dont really learn anything

2. may cause serious problems in your code later

and as soon as you understand how everything works perfectly to use these little helpers ...

I went through the same thing when I was studying html, flex, swing and cocoa touch ... and this may take a little longer at the beginning, but as soon as you understand it and can write helper classes / methods, everything will work very quickly and good for you.

 Just found some usefull information/tips about xib: 

Keep your beer files small. Most new projects in Xcode come with one or two pre-configured nib files. The mistake made by many developers who are new to Interface Builder is to put all application windows and menus in one or two nib files. The cause of the error is often convenience. (A template project typically downloads preconfigured nib files automatically, which eliminates the need for developers to add more nib download code.) Unfortunately, relying on this convenience can often lead to poor performance and added memory pressure to your application.

When a nib file is loaded into memory, this is an all-or-nothing attempt. The nib download code has no way of knowing how many objects in a file or which of them are important, so the whole file must be loaded into memory. An instance of individual objects is then created from this data in memory. For cocoa and iPhone applications, all objects in the nib file are instantly created so that output and action connections can be restored. If your application first uses only some of the nib file objects, all objects in memory are waste.

For all projects, it is better to design each nib file so that it contains only those objects that are needed immediately in this situation. When loaded into memory, such a nib file uses the smallest amount of memory, although it has everything it needs to complete the job. Here are a few design options to consider when organizing your nib files:

For your main nib application file, specify only the menu bar (or, in the case of the iPhone application, only the main window).

For document file files on Mac OS X, you only need to enable the document window and the objects (such as controllers) needed to display this window.

For other nib files, focus the nib file on a key object, such as a single window or panel that you want to display. All other objects in the nib file should then facilitate the immediate operation of this window or panel.

For windows that change their embedded view hierarchies, if the hierarchy does not change often, consider storing any additional hierarchies in separate nib files. Download each view hierarchy only as you use it.

If you already have large nib files, you can use Interface Builders refactoring tools to split them into several smaller nib files. For information on how to use refactoring tools for interface builders, see the "Refactoring Your Files Files" section. For information on how to explicitly load nib files from your code, see the Resource Programming Guide.

But my personal opinion as a professional programmer (like I am a software developer) is that it is always better for beginners to start from the software path, just to find out and understand how everything works

+5
source

All Articles