IOS development NOTE 2: singleton, iossingleton

Source: Internet
Author: User

IOS development NOTE 2: singleton, iossingleton

Each app has only one UIApplication, and a class like UIApplication "[UIApplication sharedApplication]" has a unique instance design, that is, the singleton mode.

Singleton design and key points

Shows the design of a singleton class.

Note the following points:

(1) The instance must use static to prevent modification in other files;

(2) In view of the development habits, class methods are named after "sharedXXX" and are uniquely instantiated;

(3) The allocWithZone method is called during alloc. To keep the Singleton, You need to override this method;

(4) checking whether the two instances are nil has different meanings. For the first time, check whether the instance is nil, that is, the so-called double check lock, to avoid repeated locking. Because after the first instance is instantiated, there is no need to lock the instance each time you obtain the instance, And you can directly return to the instance. The second check whether the instance is nil is obviously to ensure that the instance is unique.

(5) Locking @ synchronized ensures thread security and prevents multiple threads from simultaneously obtaining instances when _ instance is nil. Therefore, multiple instances are generated at the same time. Lock to ensure that only one thread can be called at the same time, and ensure the singleton;

(6) In order to prevent new instances from being generated during copy, copyWithZone is also re-executed to ensure the singleton

GCD implementation Singleton

Shows the GCD method Singleton design.

Static

Static modifies global variables to restrict their scopes so that the variables are valid only within the defined source file.

Otherwise, if we remove the static modifier, We can reference other external files and modify the variables, which destroys the Singleton.

 

Use Case in MRC Environment

In the MRC environment, in addition to the above design, some overwrites are required to make the object reference count always 1.

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.