<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>bunnyhero dev &#187; memory</title>
	<atom:link href="http://www.bunnyhero.org/tag/memory/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bunnyhero.org</link>
	<description>Notes on iPhone, Flash and Web development</description>
	<lastBuildDate>Mon, 07 Nov 2011 21:49:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Too many monkeys! A newbie lesson about viewDidLoad:</title>
		<link>http://www.bunnyhero.org/2009/03/23/too-many-monkeys-a-newbie-lesson-about-viewdidload/</link>
		<comments>http://www.bunnyhero.org/2009/03/23/too-many-monkeys-a-newbie-lesson-about-viewdidload/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 17:44:56 +0000</pubDate>
		<dc:creator>bunnyhero</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[initialization]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[newbie]]></category>
		<category><![CDATA[uiview]]></category>
		<category><![CDATA[uiviewcontroller]]></category>
		<category><![CDATA[viewdidload]]></category>

		<guid isPermaLink="false">http://www.bunnyhero.org/?p=144</guid>
		<description><![CDATA[Why was my app spawning extra monkeys every time I went back and forth between the main view and settings view? Find out here.]]></description>
			<content:encoded><![CDATA[<p>I recently learned another important newbie lesson about <code>viewDidLoad:</code> : it may be called <em>multiple times during your application&#8217;s lifetime</em> for any given <code>UIViewController</code> if its view is ever hidden and shown during low-memory situations.</p>
<p>Here&#8217;s the story: I am currently making a monkey app which features a single monkey. It was running fine in the simulator, but sometimes when I ran it on the device, an extra monkey was spawned every time I switched between the main view and the settings view! A couple of view switches and I&#8217;d have a bunch of monkeys onscreen, which was amusing but also very slow and not what was intended. Who was making the new monkeys?</p>
<p>My first iPhone test apps did a lot of initialization in <code>viewDidLoad:</code>. It makes sense because it is called right after a view has finished loading from its nib, and it feels like a good time to do your final initialization of things since the system has basically done everything it needs to do to display a window.</p>
<p>What I forgot was that the default behaviour of <code>UIViewController didReceiveMemoryWarning</code> is to release its view if it&#8217;s currently hidden (due to navigation, say). All well and good. But when the view becomes visible again, the view will be reloaded from its nib, and <strong><code>viewDidLoad:</code> will be called <em>again</em></strong>.</p>
<p>This is why I kept getting new monkeys: I was making the monkey in my <code>MainViewController</code>&#8216;s <code>viewDidLoad:</code>. When I switched to the settings view when memory was short, the <code>MainViewController</code> purged its view. Switching back to the main view caused the <code>MainViewController</code> to reload the view from the nib, thus calling <code>viewDidLoad:</code> again, and thus creating another monkey.</p>
<p>So, any initialization code that goes into <code>viewDidLoad:</code> should make sense in that situation: i.e. view-specific stuff when recreating the UIView object, <em>not</em> application-level initialization stuff. Or, if your app-level initialization stuff needs a view to be loaded before running, then at least check to make sure that it hasn&#8217;t run already.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bunnyhero.org/2009/03/23/too-many-monkeys-a-newbie-lesson-about-viewdidload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

