Forrest
Forrest

Reputation: 127673

Unable to simulate invoking applicationDidReceiveMemoryWarning:?

Here is the calling code:

[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationDidReceiveMemoryWarningNotification
                                                    object:[UIApplication sharedApplication]];

It can not invoke applicationDidReceiveMemoryWarning: from UIApplicationDelegate.

Anything wrong?

Upvotes: 2

Views: 700

Answers (3)

omz
omz

Reputation: 53551

You cannot simulate a memory warning by posting a notification. The UIApplicationDidReceiveMemoryWarningNotification is posted by UIApplication when it receives a memory warning, but it doesn't observe it, and therefore doesn't call your view controllers' didReceiveMemoryWarning: method when you post such a notification manually.

As Rob already pointed out though, you can simulate a memory warning in the iOS simulator by using the "Simulate Memory Warning" menu item.

You could also observe the notification in your view controllers, instead of implementing didReceiveMemoryWarning:, but I wouldn't recommend that, because the behavior of system-supplied view controllers might be different when you fake a memory warning that way.

Upvotes: 0

Rob van der Veer
Rob van der Veer

Reputation: 1148

In the simulator, use the menu to trigger a low memory warning.

enter image description here

Upvotes: 2

szzsolt
szzsolt

Reputation: 629

What should work is using UIApplicationMemoryWarningNotification instead of UIApplicationDidReceiveMemoryWarningNotification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification"
        object:[UIApplication sharedApplication]];

Upvotes: 0

Related Questions