[ext] 25 tips and tricks for enhancing the performance of iOS apps-Advanced

Source: Internet
Author: User
Tags epoch time uikit

_____________
Transferred from: Beyondvincent's Blog
_____________

When developing an iOS application, it is critical that your program has good performance. This is also what users expect, if your program runs slow or slow, will incur the user's bad comment.

However, due to the limitations of iOS devices, it is sometimes difficult to get good performance. There are a lot of things to keep in mind during the development process, and it's easy to forget about the performance impact.

That's why I'm writing this article! This article collects 25 tips and tricks that can improve program performance.

Directory

I've divided the performance optimization techniques into 3 different levels: Beginner, Intermediate, and advanced:

Senior
Use them when and only if the following techniques are able to solve the problem:

    1. Accelerated start-up time
    2. Using Autorelease Pool
    3. Cache pictures-or do not cache
    4. Try to avoid date formatting
Advanced Performance Improvements

Looking for some clever ways to turn yourself into a full code ninja? The following advanced performance optimization techniques allow the program to run as efficiently as possible at the right time!


22) accelerated start-up time

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

The way to get the program started as quickly as possible is to perform tasks asynchronously, such as network requests, data access, or parsing.

Also, avoid using bloated xibs, because Xib is loaded in the main thread. But remember that storyboard has no such problem-so use storyboard if you can!

Note: When debugging with Xcode, the watchdog does not run and does not connect the device to Xcode when the test program starts performance on the device.


23) Using Autorelease Pool

The NSAutoreleasePool is responsible for releasing an automatically freed object from a block of code. are generally created by Uikit. However, there are cases where you need to create nsautoreleasepool manually.

For example, if you create a large number of temporary objects in your code, you will notice that memory usage is increasing until those objects are freed. The problem is that these objects are freed only when Uikit runs out of autorelease pool, meaning that these objects still occupy resources in memory when these objects are no longer needed.

However, this problem is completely avoidable: Create a temporary object in the @autoreleasepool code block, as follows:

Nsarray *urls = <# An array of file URLs #>;for (Nsurl *url in URLs) {    @autoreleasepool {        nserror *error;
   nsstring *filecontents = [NSString stringwithcontentsofurl:url                                         encoding:nsutf8stringencoding error:&error ];        /* Process The string, creating and autoreleasing more objects. */    }}

When each iteration is completed, all Autorelease objects are released.

For more on NSAutoreleasePool, you can read Apple's official documentation.



24) Cache Pictures-or do not cache

There are two ways to load UIImage from program bundles in iOS. The first is more common: imagenamed. The second method is seldom used: imagewithcontentsoffile

Why are there two ways to do the same thing?

The advantage of imagenamed is that you can cache images that have already been loaded. Apple's documentation has the following statement:

This method looks in the system caches for a 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 I T, and then returns the resulting object.

This method looks for the image in the system cache according to the specified name, and returns if found. If a picture is not found in the cache, the method loads the picture data from the specified file, caches it, and then returns the result.

The Imagewithcontentsoffile method simply loads the picture and does not cache the picture.

These two methods are used in the following ways:

UIImage *img = [UIImage imagenamed:@ "MyImage"]; caching//oruiimage *img = [UIImage imagewithcontentsoffile:@ "MyImage"]; No caching

So how do you choose?

If you load a large picture and use it only once, you do not need to cache the image. This is imagewithcontentsoffile appropriate-the system does not waste memory to cache pictures.

However, if you often need to reuse images in your program, it is best to choose the Imagenamed method. This method saves the time it takes to load pictures from disk each time.


25) Try to avoid date formatting

If you have a lot of dates to use NSDateFormatter, you need to be careful. As mentioned in the previous (expensive reuse object), whenever possible, you should try to reuse nsdateformatters.

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 provided some string codes for parsing ISO-8601 format date characters. You simply need to tweak the code to meet your specific needs.

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

If you can control the format of the date, you can choose Unix timestamps. Unix timestamps is a simple integer that represents the number of seconds from the epoch time to the present, usually the new epoch reference time is 00:00:00 UTC on 1 January 1970.

You can easily see this timestamp converted to NSDate, as follows:

-(nsdate*) Datefromunixtimestamp: (nstimeinterval) Timestamp {    return [nsdate dateWithTimeIntervalSince1970: Timestamp];}

The above method is faster than the C function!

Note: Many network APIs return timestamps in milliseconds, so it is important to be aware that you need to divide this timestamp by 1000 before passing it to Datefromunixtimestamp.

Go?

Readers interested in program performance optimization are strongly advised to look at the WWDC videos listed below. Before you watch the video, you'll need to register an Apple ID (just sign up to see all 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 and are also 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

Here are more videos, most from the iOS 5 technical lectures:

    • Your IOS App Performance hitlist
    • Optimizing APP Performance with Instruments
    • Understanding IOS View Compositing

Based on the "Your IOS App performance hitlist" video, Ole Begemann wrote an article.

Apple also offers a very good article: performance optimization. The tips and hints provided are useful for improving program performance.

[ext] 25 tips and tricks for enhancing the performance of iOS apps-Advanced

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.