IOS development Advanced Interview Questions-Role of dealloc under ARC,-arcdealloc

Source: Internet
Author: User
Tags notification center

IOS development Advanced Interview Questions-Role of dealloc under ARC,-arcdealloc

In the MRC era, we need to do a lot in dealloc, such as releasing objects;

Now we have entered the ARC era. The system has helped us with the release of common objects;

Can I rest assured? What should we do under ARC?


Opinion:

Under ARC, the system can help us release this object and its contained objects;

However, some things that do not belong to this object cannot be released, such:


1. Notification observer


Because the notification center is a single example of the system, when you register the notification observer, you actually register in the notification center,

At this time, even if the system under the ARC helped us release the object, but the observation in the notification center is still not removed, when there is

This notification still calls the method for receiving the notification from this object, which may cause some problems.

This is a bit like, you get off work at six o'clock, but you didn't swipe your card ...,


2. Release of strong Commission/reference of static objects (for example, XMPPMannerger's delegateQueue)


When you use other objects as delegate and are strongly referenced, you are immediately released, but your object is still being referenced,

In this case, you need to remove the delegate When referencing your object.


3. Perform other operations


An object, such as a ViewController, may need to deal with the server before it is destroyed;

In this case, we can also write in dealloc


For the underlying implementation of dealloc, refer to the objc runtime code.


For example, what we should do in ARC:

-(Void) dealloc {[[nsicationcenter center defaultCenter] removeObserver: self]; // remove notification observer [[XMPPManager sharedManager] removeFromDelegateQueue: self]; // remove the delegate reference [[MyClass using instance] doSomething] // other operations}


We don't have to do anything under ARC.


- (void)dealloc{    _name = nil;   [_time invalid];}


Conclusion: The Role of dealloc in ARC is to release resources occupied by this object that cannot be released by the system, or reference other objects to this object.


From: http://blog.csdn.net/yangbingbinga/







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.