Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

iOS Development: Tips for effective development in iOS

This article will present some tips and best practices for developing applications on the iOS platform that avoid memory leaks and bottlenecks in order to ensure the best possible user experience.

The response that the system gives to the user on a mobile application when he/she does something is extremely important. It is totally undesirable, for instance, that users became unsure of what is happening while the system search for something on the internet. In addition, users don’t appreciate when the system is unresponsive to scroll’s gestures when something is being done by the application.

The development of an app that allows an enjoyable use is something that involves many variables ranging from the right choice of images to the efficient use of the mobile’s available resources. Therefore, the knowledge of concepts like blocks, memory leaks, Activity Monitor, Time Profiler and the GCD becomes extremely important, as they, together, allow you to createresponsive applications.

As an example, the Activity Monitor allows you to check the allocation of resources including free and available memory. With the Time Profiler it is possible to identify where your App is taking longer time to respond, which functions are freezing the application and makingthe user wait.

Scenarios where this topic is useful

Any application must be developped by taking into account the user's attention and their willingness to remain in or return to the application. A sloppy or poor application will not captivate the users and they will look for an alternative.

The mobile iOS applications created based on the practices presented in this article will be more stable, leaner, and responsive.

The software development task has a number of practices that aims a final result: a polished product that will end up being used by many. However, when we start the development we often note that this is not a simple task

The marketing and sales of iOS applications become much simpler with the iPhone arrival in 2007 and the AppStore in 2008, since after those initiatives it is no longer necessary to input costs with boxes, media, and transportation logistics (not to mention negotiating with distributors). Also, new opportunities arise for international sales, an option that was possible only to large companies, small specialized development shops and private tutors. Moreover, this openingbrought good and bad aspects. The downside is that the number of people developing applications increased incredibly. This is not bad due to the competition, but because many of these people are not trained for developing iOS Apps.

The goal of this article is not to discuss differences (ideological or not) among the platforms and languages. The type of applications (Apps) discussed here are products developed for mobile devices such as smartphones and tablets and not the type of products targeted for desktop and servers with or without a web interface.

There is a big difference between types of applications mentioned and they should not be mixed. A desktop is almost limitless in itscapabilities and settings. A server is more flexible especially when Cloud Computing (a platform that can have multiple machines working together to perform a task) is used.

The iPhone and the iPad have a memory of 512 MB or 1 GB and 128 GB of maximum storage space. For comparison purposes, a desktop computer used on daily tasks have 16 GB of memory and 5TB of storage spread across multiple disks.

Moreover, the desktop has an operating system that is swapped (uses part of the disk as memory, to allow the use of more applications at once without performance degradation). That means at least 16 times more storage space and memory than smartphones.

What does this mean in practice? Well, the memory limitation of mobile devices, the lack of swap and the small storage space force developers to create Apps that areleaner and well finished. An important note here: lean not mean smaller. Lean is this context means that the developer know how to use the machine's resources well and only use them when necessary.

And this is the goal of this paper: address some of the good programming practices for creating lean apps that do not have problems with the management of the device’s resources and that have an adequate response time to the end user.

 The growth of mobile development

Figure 1: The growth of mobile development

Concepts and Tools

We are going to see a set of concepts that will help the development of iOS apps usingthe good development practices.

User interface

The iOS Human Interface Guidelines document provided by Apple recommends that the touch area of an object to be at least 23 pixels and the recommended value is 31 pixels. These numbers were defined considering studies that took into account the average size of the finger of a normal adult. Thus, keep in mind the following points when you design the user interface:

  • Minimal element size;
  • The distance between elements;
  • The relative location of elements (who is on top of whom).

The responsiveness of user interface, i.e. the result that the system gives the user when he does something, is extremely important. You will not want that users be unaware of what is happening while the system will search for something on the internet or update the images on Twitter. In general, you should avoid unresponsive app, especially when users are performing scrolling gestures while something is done in background.


Blocks - a snippetof code that is characterized by the caret (^) - is a form of code which the execution is always done in a separate thread. A block allows the inclusion of routines within routines without the creation of functions or classes. The advantage of this technique is that the queue management implementation is done by the system itself, within the GCD, or Grand Central Dispatch, which is the queuing system that manages parallel code execution.


Threads are tasks executed in parallel that free the user interface to wait while creating the system is busy. Every update on the interface should always be made by the processing on the main thread.


User interface animations in any system provide a pleasant feeling and are also a great incentive to use them. The concepts of classical animation are used long ago and aim to simulate what happens in real life.

Some examples of animations in applications are the bounce scroll (the spring as it is called), screen fade in/out (appearance), and the appearance movement of the keyboard, among others.

Another example of how to improve usability, especially on touch screens where there is no tactile response to what the user is doing,is the sounds played that inform that the application is responding to user actions. The best examples are the sounds played when the keyboard is used and when some application successful sends an e-mail.

Memory Leak, Garbage Collection and the ARC

Everything that a computer do (and mobile devices are computers too) is done using memory. Each open program occupies a specific area of memory and every time a new activity is going to occur new storage spaces are created (allocated). If the programmer does not take care to clean these spaces by releasing the used freeing memory there will be no more resources to execute anything more and the application will crash.

When a certain function finishes its execution and its variables were not properly treated (cleaned) a memory leak will happen. Here, the example of a glass is perfect, since we can image a memory leak just like water leaking from a glass.

Suppose we have a large bowl with several glasses that contain holes. If we pour water on the glasses until we reaching the hole the water begins to leak into the bowl. After a while several cups are going to leak and that will make the basin full of water. In our analogy each cup is a program, the water is the memory allocation and the leak is the lack of proper treatment of program variables. Every time a leak occurs the device memory (basin) will be filled until it runs out. The iOS operating system notifies each open application that there is a problem with the memory and it is the responsibility of the program to cleanse the variables that are no longer being used. If that cleansing doesn’t happen the operating system will takes care of closing the application due to lack of memory.

Until the iOS version 4 the management of memory in a program was the sole responsibility of the programmer. The iOS 5 introduced to new concepts to manage memory allocation: The Garbage Collection (GC) and the ARC.

Garbage Collection is literally the use of garbage, i.e. the operating system creates an area where variables are created and the operating system itself takes care of cleaning them. One can say that they are temporary variables. Programmers must take care to determine which variable is permanent (retained) and which ones are temporary.

ARC stands for Automatic Reference Counting and it means that the operating system will take care of knowing when a variable is being used and when it can be thrown away by the Garbage Collection.

Thus, the programmer that used ARC does not need to worry about managing the memory as this is done by the system. However, it is up to the developer to ensure that the variable exists when required.


The iOS devices resources, i.e. the memory and the disk storage, are very limited. The first versions of the iPhone had only 128MB of memory. Nowadays the iPhones 4 and 4S and the iPad 2 and Mini have 512MB of memory. The iPhone 5 and iPad 3 and 4 have 1GB of memory. These devices can have 16GB, 32GB, 64GB or 128GB or disk storage space and the average size of most devices sold is around 32G. Both resources are shared among all installed Apps plus the operating system itself and the contents of each App like photos, videos and music.

This means that the memory available for your App is restricted. More restricted than it seems, since around 70 to 80MB of memory is solely reserved for system use. In early versions of iPhone this meant that less than 50% of memory was available for all Apps.

With such limitation how doesthe developer avoid the App slow down and crash due to lack of memory? The following sections show some tips that can help avoid these problems.

Memory management

Each variable that is created in the system allocates a memory space which can be small, such as an int (integer - which requires 32bits), or big, as a NSMutableDictionary with 1,000,000 (one million elements) variables. But without these variables, your app does not do anything, right? So the first tip is: delete the resources that are no longer being used.

Another tip to avoid overloading the system is not using literal, i.e. the direct association of a variable type such as:

NSString *text = @"my text";
Instead, initiate the variable as follows:
NSString *text = [[NSString alloc] initWithString:@"my text"];
{do what is necessary with this variable }
[text release];
text = nil;

Proceeding this way you will have control over the memory allocation and when it can be released. If possible, always assign the nil value to variables after their use. This will reset the space allocated freeing up resources for the system.

Also, use of the @autoreleasepool: it is mainly used in loops and manages memory automatically. For example, several variables are created inside the loop and at the end of the loop the @autoreleasepool undertakes to clear these variables and their contents. It is widely used in data processing to XML parsing.

Finally, try to respond to memory warning events. Release the resources that are possible. Whenever you got a didReceiveMemoryWarning check what features should or should not be of the retain type (or retained in memory for later use). Verify that resources can be recreated when needed and if you got a didReceiveMemoryWarningrelease those resources.

Lazy load

Use the lazy load approach to load content (especially images) only when necessary and,if possible, reuse this object. For example, try to reuse the cell of a table, i.e. the cell structure and its design objects (not the data itself). If you need to load an image from the internet do it in parallel - using blocks or asynchronous communication - and release the user interface for the user to perform other actions.


Take care of all images that will be used in the App by choosing the PNG format since this type of image can be compressed by the App and decreasing its size.

If you need to upload an image choose the [[UIImage alloc] initWithContentsOfFile: @ ""] methodinstead of the [UIImage imageNamed: @ ""] because this first one dos notinsert the image in the cache.

If you have to load a giant picture try cutting it into pieces and load only the part of image that is visible. On the Apple website there is an example about this under the ScrollViewSuite topic.

Whenever you can, decrease the images’ sizes and its resolutions. The iPhone and iPad have a 72dpi screen resolution. If you do not need to zoom in the image the image should not to be larger than the size of the device screen size. Make the image the size of the element when customizing buttons or other interface elements.

Always create two images: one for the normal size element and another twice the size (@ 2x) for retina display devices.

Try to avoid images with transparent background. Use the image’s background to optimize performance. The use of transparent objects is CPU intensive and it is very expensive for the processor.

New technologies

Keep your App up to date with the new XCode and iOS features. Use blocks, GCD (Grand Central Dispatch) and ARC. Also, always use the Static Analysis Tool. This tool does a pre-check in your code and report variables that are not being used nor released among other useful information to improve your code.

The code logic and the operation of the App is the developer’s responsibility, but the constant use of these technologies will help avoid the most common mistakes and prevent the developer from forgetting best practices, such as modification of an array that was declared at the beginning of the method. The Static Analysis Tool will let you knowabout this and show where the problem lies.


Instruments is a Xcode tool that allows themonitoring of various device’s items of the running programs. The use of Instruments is essential to check memory leaks, memory consumption and execution time of each function.

Learning to use the Instruments is essential to deliver a relatively stable App. However, neither Instruments nor XCode can solve logic problems, as this it is the programmer’s responsibility.

Use Instruments to improve the App’s speed and response time and to conserve battery powerthough efficient monitoring of resources.

Try to use at least the Leaks, Time Profiler and Activity Monitor functions of the Instruments tool. The Leaks function can show, at runtime, where memory leaks are occurring, i.e. where a variable was created, used and not released. Remember: more leaks means that there is less available memory for your App and for other apps and the system.

Using the Activity Monitor you can check the allocation of resources, mainly the available memory. A crash may occur due to lack of memory if your App consumes a lot of memory during its execution. Check all parts of your app and check how the memory is consumed after a Controller is created and is being released properly after it ceases to exist. If the variables are not being properly releasedmore and more resources will be allocated avoiding proper memory managementevery time theControlleris being used.

The Time Profiler function allows you to see where your App is getting longer, which functions are freezing your App and make your user wait. Optimize the execution of that part of the code. Use blocks, GCD, threads and other optimizations to reduce the execution time of this particular function and release system resources, particularly the user interface.


Dealing with the user interface is essential after the correct memory management and use of resources. After all, leaving the user waiting is never a good policy.

Many actions can be done to optimize the response time of your App. Among them the most important part is the render process. But what is rendering process? Render is the transformation of a lotof mathematics into a picture. Everything that you see on the screen is the result of a rendering process, image is rendered and the text is rendered. Every pixel shown on the user interface is the result of a rendering process.

Therefore, whenever you can use the system standard mechanism for drawing: the UIKit framework. It has been optimized for showing the pixels of the user interface in an iOS App.

Again, try to avoid objects with transparency because the time to calculate the necessary render operations implies a check what is behind the object, which perform the calculation twice: first for the object on front and then for the object behind.

Use PNG images of the BGRA type instead of the RGBA type, i.e. a pre-multiplied image. But, that is a pre-multiplied image? Every image is composed of three RGB (red, green and blue)color channels and a transparency (Alpha)channel. Pre-multiplied images havethat the Alpha channel is applied directly on each color channel. Check on your favorite image editing program how to do this.

When the user is doing something on the screen like a scroll try to avoid refresh the screen and show new or updated elements.

Try not to do too much processing during the App startup. The Apple usability guidelines says that every App that takes longer than two seconds to start will be refused when submitted to the Apple Store and, if it has been previously approved, it will be removed completed from the device. Therefore, delay the load of resources to be done only when they are ready to be used.

Use the CoreData framework to manage large amounts of data. The Controller takes care of CoreData to get the data when needed, while it also manages the updates CoreData objects stored in the database.

Animations and end-user response time

Everything that happens is intrinsically linked to the expectation. Suppose you are stopped in traffic not knowing for how long the traffic light is showing the red light and how much time will take to see the green light. What is your expectation?

After a timeout your anxiety begins to shown and some people even go through when the red light is still on. In this example the user’s time expectation was not met.

The same scenario may happenwhen a user is waiting for the App to complete a task. If you expect something to happen at a certain time and it does not, your expectation will cloud your judgment about the quality of the App Nowadays there are traffic lights that show how much time remains to be green or red, thusreducing thethe expected waiting time effect, since the time information (seconds remaining) is being shown.

When creating the user interface of your App do the same: place visual cues that inform what is happening.

Use the spin object (UIActivityIndicator) to indicate that the app is processing something. Use the elastic scroll to display that the content is over. Use the scroll display (Show Horizontal Scroll or Show Vertical Scroll options) to indicate the total size of the content being shown.

Use animations to improve the user experience. For instance, when updating the contents of a table do not just reload the content ([self.tableView reloadData];). Use the functions to insert, delete or update one or more table rows with animations([self. reloadSections tableView: [NSIndexSet indexSetWithIndex: 0] withRowAnimation: UITableViewRowAnimationFade];).

Similarly, use animations when an object is displayed, such as the appearance of the keyboard or a ModalViewController. Use the code shown in Listing 1 to display a View using the fade-in effect.

Listing 1: Using the fade-in effect to show a view

[_object setAlpha:0];      
[UIView animateWithDuration:0.4        animations:^{       
        [_object setAlpha:1];       

Try to observe other Apps or even in cartoons how the animation changes everything that is happening. See how transitions occur between screens. Another good example is the iBooks App: note how the transition between pages occurs. Inspire yourself.

These small touches will change the perception of users as they see your app, since the updates occur smoothly and lively and not abruptly and without feedback. Remember, it is very important to show to the user what is happening.

Finally, test you App on the device. The simulator is great for testing the programming logic and to verify that everything is proceeding according to the specifications, but it is the device that will tell if the performance is adequate, if the memory is being used correctly and finally if the App is giving the best possible experience to the user.

By the way, the simulator is optimized to run on a Mac desktop and behave like the iPhone. However, there are functions that are not possible to test in the simulator, such as the use of the gyroscope, cameras and some other hardware features. The device is the instrument that should be used to finalize the testing and guarantee that the App is working right. If you develop for multiple versions of operating systemstest you App on all of them either via simulator or on the device.


The design of the interface, its responsiveness, resource management, and attention to details are key elements to develop a mobile application.

The user does not want to wait while your application downloads a large internet content and then release the interface for other activities. Use blocks and GCD (Grand Central Dispatch) to avoid this scenario.

Also, inform the user what is happening. Use animations and animated elements, such as spin, to get the attention and, at the same time, show that the system is working and is not frozen. Make animated transitions and, when displaying an object, use animations to soften the appearance of the object.

If your app will process very large databases or even a giant image, properly manage the resources so that the app doesn’t crash and leave the user without knowing what happened. User experiences like these that make the users post negative reviews in your App that may decreases sales.

Use the Instruments tool to improve the maximum responsiveness, resource management, and response time of your App.

We saw this article that block programming and the use of GCD allows you to run tasks in parallel allowing your application to be used as some internal processing is happening. Therefore, it’s a good practice to perform complex calculations or loops in secondary threads and not on the main thread.

Transform your pictures, release variables and, when necessary, respond to the events of the operating system and release the resources that are created at run time for each function/class. Use animations to give feedback to users.

I hope that with these tips your Apps become more stable, leaner and more responsive to the user. After all, the user experience is what counts.

Microsoft MVP, Bachelor in Science And Technology, Editor and Consultant at DevMedia with more than 7 years of experience in software development.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login