tags:

views:

85

answers:

4

What is the use of viewDidUnload and didReceiveMemoryWarning methods?

When they actually get called?

what are the difference between dealloc, viewDidUnload and didrecievedmemorywarning?

+1  A: 

viewDidUnload is supposed to undo what viewDidLoad does, just like dealloc is supposed to free up whatever resources init created. So:

-(id)init {
    if (self == [super init]) {
      foo = [[Foo alloc] init];
    }
    return self;
}

-(void)dealloc {
  [foo release];
  [super dealloc];
}

-(void)viewDidLoad {
    [super viewDidLoad];
    bar = [[Bar alloc] init];
}

-(void)viewDidUnload {
  [bar release];
  [super viewDidUnload];
}

didReceiveMemoryWarning is there so you can free up any unnecessary memory. Perhaps you cache images. That's nice to have, for a snappy UI, but when memory's tight you can release that memory and your application's performance can degrade gracefully.

Frank Shearar
If I donot use viewDidLoad and release my outlets in viewDidUnload, then what will happen?
A: 

It's a good idea to start with Apple's documentation, as there is a lot there about this subject.

Alex Reynolds
A: 

viewDidUnload is called when a UIViewController subclasses unloaded it's view. The view is then released, and all IBOutlets are disconnected. The method is usually fired when a viewController is popped, or when it recieves a memory warning.

didRecieveMemorywarning is called when the OS sends a memory warning. This happens when the device is low on memory. You should try a release as much data as possible here, to free up memory. Caches, data that is not needed at the moment, etc.

If you don't free up memory, your app will be killed at a certain point to free up memory for the OS.

Dealloc is called when the instance is deallocated.

Rengers
+1  A: 

viewDidUnload: It is called when viewcontroller recieves the low memory warning.

dealloc: It is called when the object/viewController is released.

didRecieveMemorywarning: Also called when the controller recieves the low memory warning.

So Whats the difference between viewDidUnload and didrecieveemoryWarning?

viewDidUnload is the place where you need to clean up the UI related things i.e., outlets.

didRecieveMemoryWarning is a place where you need to clean up the other objects which are holding memory and not used frequently.

Manjunath