Question

Here is the calling code:

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

It can not invoke applicationDidReceiveMemoryWarning: from UIApplicationDelegate.

Anything wrong?

Was it helpful?

Solution

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.

OTHER TIPS

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

enter image description here

What should work is using UIApplicationMemoryWarningNotification instead of UIApplicationDidReceiveMemoryWarningNotification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationMemoryWarningNotification"
        object:[UIApplication sharedApplication]];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top