25 tips and tips for enhancing the performance of IOS apps-advanced

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. There are three different levels: Elementary, intermediate, and advanced:

 

Advanced

Use these skills only when they can solve the problem:

22. Acceleration Start Time

23. Use autorelease pool

24. cache images-or do not cache Images

25. Try to avoid date formatting

 

Advanced Performance Improvement

Looking for some clever ways to turn yourself into a full-code ninja? The following advanced performance optimization techniques allow programs to run as efficiently as possible when appropriate!

 

22) Acceleration Start Time

It is very important to start a program quickly, especially when the user starts the program for the first time. The first image is very important to the program!

 

The best way to enable programs to start quickly is to execute tasks asynchronously, such as network requests, data access, or parsing.

 

In addition, avoid using bloated xibs because the XIB loading is performed in the main thread. But remember that storyboard has no such problems-So use storyboard if you can!

 

Note: When Using xcode for debugging, watchdog does not run. Do not connect the device to xcode when testing the Startup Performance of the Program on the device.


23) Use autorelease pool

The nyothoreleasepool is responsible for releasing automatically released objects in a code block. Generally, they are created by uikit. However, in some cases, you need to manually create the NSAID utoreleasepool.

 

For example, if you create a large number of temporary objects in the code, you will notice that the memory usage is increasing until these objects are released. The problem is that these objects will be released only when the uikit consumes the autorelease pool. That is to say, these objects occupy Resources in the memory after they are no longer needed.

 

However, this problem can be completely avoided: create a temporary object in the @ autoreleasepool code block, the following code:

Nsarray * URLs = & lt; # an array of file URLs # & gt ;;

For (nsurl * URL in URLs ){

@ Autoreleasepool {

Nserror * error;

Nsstring * filecontents = [nsstring stringwithcontentsofurl: URL

Encoding: nsutf8stringencoding error: & amp; Error];

/* Process the string, creating and autoreleasing more objects .*/

}

}

After each iteration, all autorelease objects are released.

 

For more information about the NSAID utoreleasepool, read Apple's official documentation.


24) cache images-or do not cache Images

In iOS, there are two methods to load uiimage from the program bundle.

The first is more common: imagenamed.

The second method is rarely used: imagewithcontentsoffile

 

Why are there two ways to do the same? The advantage of imagenamed is that it can cache Loaded Images. Apple's documents contain the following statements:

This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image
Object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.

 

This method searches for the Image Based on the specified name in the system cache. If the image is found, it is returned. If an image is not found in the cache, this method loads the image data from the specified file, caches it, and returns the result.

 

The imagewithcontentsoffile method simply loads images and does not cache them. The two methods are used as follows:

Uiimage * IMG = [uiimage imagenamed: @ "myimage"]; // caching

// Or

Uiimage * IMG = [uiimage imagewithcontentsoffile: @ "myimage"]; // No caching

 

So how should we choose?

 

If you load a large image and use it only once, you do not need to cache the image. In this case, imagewithcontentsoffile is more suitable-the system will not waste memory to cache images.

 

However, if you often need to reuse images in the program, it is best to select the imagenamed method. This method can save the time for loading images from the disk each time.


25) try to avoid date formatting

If nsdateformatter is required for many dates, be careful. As mentioned earlier (reusing objects with high costs), we should try to reuse nsdateformatters whenever possible.

 

However, if you need a faster speed, you should use C to parse the date directly, instead of nsdateformatter. Sam soffes wrote an article that provides string code for parsing date characters in ISO-8601 format. You only need to adjust the code to meet your special needs.

 

That sounds good-but do you believe there is a better way?

 

If you can control the format of the processing date, you can choose UNIX timestamps (http://en.wikipedia.org/wiki/Unix_time ). UNIX timestamps is a simple integer that represents how many seconds have elapsed since Epoch. Generally, the reference time for this new era is 00:00:00 UTC on 1 January 1970.

 

You can easily see that the timestamp is converted to nsdate, as shown below:

-(Nsdate *) datefromunixtimestamp :( nstimeinterval) timestamp {

Return [nsdate datewithtimeintervalsince1970: Timestamp];

}

The above method is faster than the C function!

 

Note: The timestamp returned by many network APIs instances is in milliseconds. Therefore, you must divide the timestamp by 1000 before it is passed to datefromunixtimestamp.

 

Where to go?

It is strongly recommended that Readers interested in program performance optimization Take A Look At The WWDC video listed below. Before watching the video, you need to register an Apple ID (after registration, you can watch all the wwdc2012 videos ):

#406: adopting automatic reference counting

#238: iOS app performance: graphics and animations

#242: iOS app performance: Memory

#235: iOS app performance: responsiveness

#409: learning instruments

#706: Networking Best Practices

#514: OpenGL ES tools and techniques

#506: optimizing 2D graphics and animation Performance

#601: optimizing web content in uiwebviews and websites on iOS

#225: Up and running: making a great impression with every launch



The following videos are from WWDC.
2011, also very useful:

#308: blocks and Grand Central Dispatch in practice

#323: introducing automatic reference counting

#312: IOS performance and power optimization with instruments

#105: polishing your app: tips and tricks to improve the responsiveness and Performance

#121: Understanding uikit Rendering

 

More videos are available here, mostly from IOS
5 technical lectures:

Optimizing app performance with instruments

Understanding IOS view compositing



Based on the "your iOS app performance hitlist" video, Ole
Begann wrote an article. Apple also provides an excellent article on performance optimization. The skills and tips provided are helpful for improving program performance.

 

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.