[IOS development Notes]: execution time of viewdidunload and dealloc

Source: Internet
Author: User

There is no problem with this, it is crazy release. But today I found that the original simulator could simulate a situation where the memory is insufficient.ProgramSome errors have occurred, and I have learned more about the two.

Viewdidunload is executed only when the memory is insufficient and is executed on all non-currently displayed controllers. You can use the simulator to execute: hardware to Simulate Memory warnings. When executed When the controller of viewdidunload is re-displayed, it is re-executed. Viewdidload. For example, a push B triggers a memory warning (automatically executed by ). Viewdidunload ) Pop B (then a re-executes Viewdidload ).   Dealloc is executed when the object is destroyed. For example:

A= [[A alloc]Init];

[A release];// At this time, a executesDealloc, but not executedViewdidunload

As you can see,Viewdidunload andThere is no sequential relationship between dealloc execution. Then you may have another question: InInside viewdidunload

 

 

Self.Outview=Nil;
Self.Loaddingicon=Nil;

InDealloc

 

[_ Outview release];
[_ Loaddingicon release];

 

For the differences between the two operations, see my previous note [iOS development Notes]: @ property and member variables.

 

Setting a pointer to nil is used to prevent the program from crash from accessing a released object, and sending a message to a null pointer will return 0 without triggering a crash.

Think about the example above.When viewdidunload is used, if only the Member is release, and when other places access the member, the program directly crash because the object of the address pointed to by the pointer has been release.

However, whenDealloc, there is no possibility of such a crash, so it is more efficient to disable nil.

 

Here, we should be able to understand what they should put.

 

As the Internet saysViewdidunloadYou can release something that is easy to re-build, such as local images.Viewdidload must be rebuilt.

 

For things that are not easy to re-build, such as re-download data from the server, it can be built in init,Do not release viewdidunload.

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.