For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
Contents .............................................................................................................. v
About the Author ................................................................................................ ix
About the Technical Reviewer ............................................................................ x
Acknowledgments ............................................................................................. xi
Preface ............................................................................................................. xii
■Chapter 1: Introduction to iOS Performance Optimization .............................. 1
■Chapter 2: Benchmark Your Apps with Tools: Simulators
and Real Device Test ..................................................................... 7
■Chapter 3: Increase and Optimize UITableView Performance ....................... 39
■Chapter 4: Increase App Performance Using Image
and Data Caching Techniques ..................................................... 59
■Chapter 5: Tune Your App Using Algorithms and Data Structures ................ 87
■Chapter 6: Improve Parallel Data Access using
Multithreading Techniques ........................................................ 137
■Chapter 7: Optimize Memory Usage for Better Performance ...................... 177
■Chapter 8: Integrate Multithreading and Efficient Memory Usage for
Multitasking Apps Performance ................................................ 197
■Chapter 9: Improve Performance with Native C/C++ .................................. 219
■Chapter 10: Comparing Android and Windows Phone
Performance Problems ............................................................ 241
Index ............................................................................................................... 265
iv
Chapter
1
Introduction to iOS
Performance Optimization
This chapter will introduce general information about the book, including the following:
Who this book will best serve
The topics this book will cover
The general structure and style of the book
A New Era of Smartphone
There are currently hundreds of thousands of iOS applications on the market and
hundreds of millions of iOS users, making this a big market for any company or
developer to explore. This market has been growing for many years and will keep
growing in the next few years, as will the need for interesting and powerful applications.
If you have a good idea for a new app, you need to make sure that the idea is
implemented well; this includes creating a good user experience. Because of the unique
technical limits of the Smartphone environment, good performance is a must for your
application. People want an app that responds quickly to their interactions, one that can
compute data and visualize it immediately.
Why Performance Matters
Performance is not just about algorithms, data structure, and memory. It’s about making
people feel that the application responds to any interaction as fast as possible.
Therefore, performance optimization in your iPhone application is important. Users have
to feel that they are interacting with real agents that receive their command and execute
it almost immediately. What if you tap a button and two long seconds later, you see the
effect. Are you happy with this performance? If you’re not happy, your users are
probably even more frustrated.
1
2
CHAPTER 1: Introduction to iOS Performance Optimization
Of course, you can shift much of your storage and processing into the cloud where there
are thousands of servers that can compute and return the result quickly. However, it’s
not enough to just put all your data and every computation into the cloud. Network data
transfers are tricky and your users will still probably need to wait for couple of seconds
before their data arrives.
Whether you are a game developer or a general application developer, you are likely to
experience difficulties in improving the performance of your applications.
Who Should Use This Book?
This book is written mainly for beginner and intermediate iPhone developers who
already know basic iPhone programming. If you’re a performance lover and want to
create an application on this new platform that is responsive and market-ready as well
as innovative, this book is for you. Even advanced iOS developers can benefit from this
book.
If you intend to go deeply into the Smartphone application programming world, this
book teaches you enough so that you can apply what you know with iOS to Android and
Windows Phone environment.
My Teaching Style
I believe that the learn-by-doing principle is the best way for a programmer to develop
skills. This book is based on that idea. I discuss general and deep practices that stem
directly from around two years of iPhone development experience and many years of
Java development experience and training. The problems that I put before you will help
you to avoid or fix many of your performance mistakes in iPhone development. I have
chosen these problems based on experience and research into the issues popular on
forums and social networks (such as Stack Overflow). I’ve identified common pitfalls and
provide the information you need to avoid these errors.
The book is a combination of three things: basic concepts, story illustrations, and
sample source code. Instead of just supplying an ad hoc tool for your specific problem, I
hope to provide you with strong skills to use in your daily iPhone programming life. I
employ different approaches to communicating concepts: sometimes an image is worth
a thousand of words, some concepts are best explained by sample code, and some
require those thousand words.
One of the best ways to start learning about performance is to develop a cool
application that you love. This practical experience will teach you more than some non-
realistic and very forgettable examples.
You don’t need to know a lot about Cocoa Touch Framework because I explain the
basic syntax and classes that you’ll need to improve your application’s performance.
Each chapter consists of a separate topic, some of which may already be familiar to you.
CHAPTER 1: Introduction to iOS Performance Optimization
3
You can also use this book as a general reference; whenever you have a specific
problem, you can look it up and read about the solution.
Every chapter follows a simple format: a short overview about what that chapter delivers
followed by the main sections and subsections. Each chapter concludes with a
summary that helps consolidate your knowledge and reminds you of the important
lessons, followed by some basic and realistic exercises so that you can have fun
practicing what you just learned.
What Do You Need?
As an iOS developer, you need a Mac OS with Xcode installed. There is a free Xcode
version from the iOS Developer Account, or you can download it directly from Apple
Mac AppStore. You also need a copy of this book plus all of the sample code, which
you can download from the Apress website. The sample projects were well tested on
Xcode 4.2, with ARC turned on, so you can run my sample projects in that environment
without any concern.
You can and should run every example to understand more about the illustrated
concepts. There are some short blocks of code that aren’t associated with any project;
you should run that code, too.
How to Use This Book
Although the chapters are not closely related, reading the book from beginning to end
will ensure that you have a solid knowledge of iPhone performance, optimization skills,
and techniques. There may be some dependencies and references between chapters.
The later chapters were written with the assumption that you have read or know about
some previous chapters.
I also recommend reading each chapter from beginning to end. Each chapter opens with
a quick conceptual introduction to the topic; then theory and practical iPhone samples
are combined to help you to understand the topic thoroughly.
You should read the summary section carefully because it reminds you of the key
knowledge that you should retain. I also recommend finishing all of the exercises as
these will help cement your new knowledge.
An Overview of the Book
This book contains a good mix of basic concepts plus practical knowledge,
techniques, and tips that will help you to be successful in the competitive iOS
development world. The book’s nine chapters cover nine different approaches
to solving performance problems in iOS development.
4
CHAPTER 1: Introduction to iOS Performance Optimization
Chapter 2: The introduction to a range of tools and instruments so that
you know how and when to use them. Many developers don’t use
these tools correctly because they simply don’t know that they exist.
Chapter 3: As an iOS developer, you will definitely use TableView in
almost all of your projects, from trivial ones to complex ones, to
display a list of data or options. The problem with the architecture of
UITableView is that when you start customizing it, the scrolling
performance suffers. You will definitely have this issue, even if in a
subtle way. This chapter gives you a list of tools and techniques to
improve your TableView scrolling.
Chapter 4: You may believe that most performance issues can be
solved using cloud computing and by simply adding more servers to
your system. Even if that’s true, network data transfer will always be
an issue. Data transfer will remain a bottleneck for years. You should
understand how to cache data locally and in memory with a limited
environment like iOS.
Chapter 5: Data structures and algorithms in the iOS development
environment are similar and different than in other environments. You
have a high level of support from the framework with many basic data
structures like arrays, sets, and dictionaries. For some tasks, you can
simply put it to the cloud, but for other tasks, especially gathering and
processing data to make a good visualization, you still need to depend
on the iOS environment.
Chapter 6: Improving the performance of the application also means
making the application respond to users’ interaction faster. This
means not blocking the main UI thread. Multithreading can help—not
just to improve the user responsiveness but also to improve the
general performance of your application. Multithreading is a difficult
topic for any platform, and you will learn it here through a range of
illustrations, examples, and clear explanation.
Chapter 7: With the release of a new tool to make memory
management automatic, developers now can take advantage of it to
avoid common memory problems like memory leaks and crash. This
chapter focuses on how you can best use your memory, and when you
should load data in and unload data out of your memory. It also covers
the new Automatic Reference Counting (ARC) mechanism of the new
SDK to make sure you can understand and use it correctly.
Chapter 8: With iOS 4 and above, all applications can take advantage
of multitasking to improve the user experience. In fact, it’s not actually
multitasking but rather a fast app-switching mechanism (applications
can’t run in background) with some special background processing.
This chapter will help you understand what features the iOS will
support and what tasks you can process and run in background.
CHAPTER 1: Introduction to iOS Performance Optimization
5
Chapter 9: In many iPhone applications, you don’t need to use any
C/C++ code to implement features. However, when you actually need
it, especially for library integration, you will be in serious trouble. You
may not need to write your whole application in C/C++ but you do
need to understand how these languages work for any necessary
troubleshooting.
Chapter 10: By now you should have a complete picture of all the
different aspects of iPhone performance. You will definitely consider
porting your application to Android and Windows Phone soon, so in
this final chapter, I give you the whole picture on similarities between
performance problems in iOS, Android, and Windows Phone. This will
help to smooth your learning experience for new platforms.
Source Code
You should download the sample source code from the book’s page on the
Apress web site (www.apress.com) and try it on your own.
Contact the Author
If you have any questions, please email me at vodkhang@gmail.com or visit my web site at
http://vodkhang.com. I shall be happy to have a chat about iPhone performance
problems.