25 tips and skills for enhancing the performance of IOS apps)

Source: Internet
Author: User

When developing IOS applications, it is critical to ensure the program has good performance. This is what users expect. If your program runs slowly or slowly, it will lead to negative feedback from users. However, due to the limitations of iOS devices, it is difficult to achieve good performance. There are many things to remember during the development process, and it is easy to forget about the performance impact.

 

This article collects 25 tips and tips on improving program performance, and divides performance optimization skills into three different levels: Elementary, intermediate, and advanced.

 

Elementary

In the development process, pay attention to the following basic skills:

 

1. Use arc for Memory Management

2. Use reuseidentifier when appropriate

3. Set the view to opaque as much as possible)

4. Avoid bloated xibs

5. Do not block the main thread

6. Make the image size the same as that of uiimageview.

7. Select the correct set

8. gzip Compression

 

1) Use arc for Memory Management

ARC was released in iOS 5, which solves the most common memory leakage problem-it is also the easiest for developers to forget. The full name of arc is "automatic reference counting"-automatic reference counting, which automatically performs retain/release work in the code, so developers no longer need to manually process it.

 

The following code blocks are used to create a view:

1. uiview * view = [[uiview alloc] init];

2 .//...

3. [self. View addsubview: View];

4. [view release];

 

At the end of the above Code, it is easy to forget to call release. However, when using arc, arc will automatically call release in the background.

 

In addition to avoiding Memory leakage, ARC also helps improve program performance: When the objects in the program are no longer needed, arc will automatically destroy the objects. Therefore, you should use arc in the project.

 

The following are some resources for learning about arc:

Apple official documentation


Matthijs hollemans's initial arc



How does Tony dahbura use arc in the cocos2d 2.x project?



If you are still not sure about the benefits of arc, read this article: Eight myth about arc-this will convince you that you should use it in Engineering!



It is worth noting that arc cannot avoid all memory leaks. After using arc, there may be memory leaks in the project, but the main cause of these memory leaks is: Block, retain loop, poor management of corefoundation objects (usually C structures, and the code is not written.



Here is an article about what problems cannot be solved by arc.
-And how to handle these problems.

 

2) use reuseidentifier when appropriate



Use reuseidentifier as appropriate

A common error in IOS program development is that the reuseidentifier settings for uitableviewcells, uicollectionviewcells, and uitableviewheaderfooterviews are not correct.

To achieve optimal performance, when the cell is returned in tableview: cellforrowatindexpath: method, the data source of table view generally reuse the uitableviewcell object. Table view maintains a queue or list of uitableviewcell objects. These data sources have been marked as reused.

 

What will happen if reuseidentifier is not used?If you do not use reuseidentifier in the program, a new cell is configured for each row displayed in table view. This is actually a resource-consuming operation, and will affect the efficiency of table view scrolling in the program.

 

Since iOS 6, you may also want header and footer views, as well as cell and supplementary views of uicollectionview.

 

To use reuseidentifiers, when table view requests a new cell, it calls the following method in the Data source:

1. Static nsstring * cellidentifier = @ "cell ";

2. uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier forindexpath: indexpath];

 

If the uitableviewcell queue maintained by table view or the list contains available cells, an existing cell is removed from the queue. If not, create a new cell from the previously registered NIB file or class. If no cell can be reused and no NIB file or class is registered, tableview's dequeuereusablecellwithidentifier: method returns an Nil.


3) set the view to opaque as much as possible)



Set view to opaque whenever possible

 

If the view is not transparent, set its opaque attribute to yes. Why? This setting allows the system to draw the view in the optimal way. You can set the opaque attribute in interface builder or code.

 

The official documents of Apple explain the opaque attributes as follows:

This property provides a hint to the drawing system as to how it shoshould treat the view. If set to yes, the drawing system treats the view as fully opaque, which allows the drawing system to optimize
Some drawing operations and improve performance. If set to no, the drawing system composites the view normally with other content. The default value of this property is yes.

(The opaque attribute prompts you how to process the view. If opaque is set to yes, the drawing system will view the view as completely opaque, so that the drawing system can optimize some painting operations to improve performance. If it is set to no, the drawing system processes the view with other content. By default, this attribute is yes .)

 

If the screen is static, setting the opaque attribute is not a big problem. However, if the view is embedded into the scroll view or is part of a complex animation, setting this attribute will definitely affect the program performance! You can use the debug \ color blended layers option of the simulator to check which views are not set to opacity. For the performance of the program, set the view to Opacity as much as possible!

 

4) Avoid bloated xibs



Avoid bloated xibs

 

Use storyboards in iOS 5 and replace xibs. However, xibs is still useful in some cases. If your program needs to run on a device installed with a version earlier than iOS 5, or you want to customize reusable views, you cannot avoid using xibs.

 

If you must use xibs, try to make the xibs file simple. In addition, each View Controller divides the view layers of a view controller into an xibs file if possible.

 

(Note: When an XIB file is loaded into the memory, all content in the XIB file will be loaded into the memory, including images. If a view is not used immediately, the memory will be wasted. This does not happen in the storyboard, because the storyboard still instantiates a View Controller when needed .)

 

When XIB is loaded, all the images involved will be cached. If the program is developed for OS X, the audio files will also be loaded. Apple's official documentation says:

When you load a NIB file that contains references to image or sound resources, the nib-Loading Code reads the actual image or sound file into memory and caches it. in OS X, image and sound resources
Are stored in named caches so that you can access them later if needed. in iOS, only image resources are stored in named caches. to access images, you use the imagenamed: Method of nsimage or uiimage, depending on your platform.

(When a NIB file is loaded, the image or sound resources involved in the NIB file are also loaded into the memory, the NIB-Loading Code reads the actual image or sound file into the memory and keeps caching it. In OS X, image and sound resources are stored in the naming cache, so that you can access them if necessary. In iOS, only image resources are stored in the named cache. To access images, use nsimage or the imagenamed method of uiimage (depending on different systems .)

 

Obviously, similar cache operations occur when storyboard is used, but I have not found any information about the relevant content. Want to learn more about storyboard? Let's take a look at iOS 5 written by matthijs hollemans: Beginner storyboard
Part 1 and Part2.

 

5) do not block the main thread



Never perform heavy tasks in the main thread. Because the left and right tasks of uikit are all performed in the main thread, such as drawing, touch management, and input response.

The risk of doing all tasks in the main thread is: If your code blocks the main thread, the program will be slow to respond. This will lead to negative comments on the app store!

 

In most cases, the primary thread is used to perform I/O operations. These operations need to read and write external resources, such as disks or networks.

 

For network operations, you can use the nsurlconnection method as follows:

1. + (void) sendasynchronousrequest :( nsurlrequest *) Request queue :( nsoperationqueue *) queue completionhandler :( void
(^) (Nsurlresponse *, nsdata *, nserror *) Handler

Or use a third-party framework, such as afnetworking.

 

If you need to perform other types of operations with high overhead (such as performing a time-intensive computing or reading or writing to a disk), use gcd (Grand Central Dispatch ), or nsoperations and nsoperationqueues.

 

The following code uses a GCD template:

1. dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^ {

2. // switch to a background thread and perform your expensive operation

3.

4. dispatch_async (dispatch_get_main_queue (), ^ {

5. // switch back to the main thread to update your UI

6.

7 .});

8 .});

In the above Code, why is a dispatch_async nested in the first dispatch_async? This is because the Code related to uikit needs to be executed in the main thread.

Let's take a look at the tutorials in Ray wenderlich: multithreading and GCD-beginner in iOS, and soheil.
How to Use nsoperations and nsoperationqueues for azarpour.

 

6) make the image size the same as that of uiimageview



Make sure that the image and uiimageview are in the same size.

To display the image in the bundle to uiimageview, make sure that the image and uiimageview are of the same size. Because image scaling is very resource-consuming, especially embedding uiimageview into uiscrollview.

If you want to download images from the remote service, you may not be able to control the image size or scale the image on the server before the download. In this case, after downloading the image, you can manually scale the image! -- Then use the scaled image in uiimageview.

 

 
7) Select the correct set



Select the correct set

Learning to use the most appropriate classes or objects is the basis for writing efficient code. This is especially important when processing set data.

An article on Apple's official website: collections
Programming topics) -- describes in detail the classes that can be used in the set data, and under what circumstances the class is used. When using a collection, every developer should read this document.

Too long to read (tldr )? The following is an introduction to common collection types:

• Array: A list of values in order. You can quickly search by index, but it is slow to search by value. In addition, insertion and deletion are also slow.

• Dictionary: stores key/value pairs. You can quickly search by key.

• Sets: A list of unordered values, which can be quickly searched based on values. In addition, insertion and deletion are faster.

8) Use gzip Compression



Use gzip Compression



More and more programs depend on external data, which generally comes from remote servers or other external APIs. Sometimes you need to develop a program to download some data, which can be XML, JSON, HTML or other text formats.

 

The problem is that the network on a mobile device is uncertain. The user's device may be in the edge network for one minute, and then in the 3G network. In any situation, do not let the user wait.

 

There is an option that can be optimized: Use gzip to compress the data transmitted over the network, which can reduce the file size and speed up the download. Compression is especially useful for text data because the text has a high compression ratio.

 

In iOS, if nsurlconnection is used, Gzip compression is supported by default, and the framework Page Based on nsurlconnection supports gzip compression, such as afnetworking. Some cloud service providers even provide compressed response content, such

Google App Engine.

 

Here is an article about gzip compression that describes how to enable and support gzip compression on an Apache active IIS server.

 

 

 

Source: ship breaking blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.