<?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>.:. blog.screenshot.at .:. Flash, Flex, RIA .:. &#187; Asynchronism</title>
	<atom:link href="http://www.screenshot.at/blog/tag/asynchronism/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.screenshot.at/blog</link>
	<description>This blog is about Flash, Flex, RIA related stuff</description>
	<lastBuildDate>Mon, 11 May 2015 18:06:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>The infamous callLater()</title>
		<link>http://www.screenshot.at/blog/2009/05/05/the-infamous-calllater/</link>
		<comments>http://www.screenshot.at/blog/2009/05/05/the-infamous-calllater/#comments</comments>
		<pubDate>Tue, 05 May 2009 23:03:24 +0000</pubDate>
		<dc:creator><![CDATA[Manfred Karrer]]></dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flashplayer]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[SDK]]></category>
		<category><![CDATA[anatomy of a frame]]></category>
		<category><![CDATA[Asynchronism]]></category>
		<category><![CDATA[AVM2]]></category>
		<category><![CDATA[CallLater]]></category>
		<category><![CDATA[Half-Fram]]></category>
		<category><![CDATA[Layout Engine]]></category>
		<category><![CDATA[Layout Mechanism]]></category>
		<category><![CDATA[Life-Cycle]]></category>
		<category><![CDATA[Marshalled Slice]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Phased Instantiation]]></category>
		<category><![CDATA[player events]]></category>
		<category><![CDATA[player render]]></category>
		<category><![CDATA[prerender event]]></category>
		<category><![CDATA[Render]]></category>
		<category><![CDATA[Sean Christmann]]></category>
		<category><![CDATA[UsePhasedInstantiation]]></category>
		<category><![CDATA[user code]]></category>

		<guid isPermaLink="false">http://www.screenshot.at/blog/?p=298</guid>
		<description><![CDATA[At my first posts I wrote about my favorite Flex feature: Databinding Now I will take a look at the opposite, IMHO the most precarious &#8220;feature&#8221; in Flex: callLater() If you have a dodgy problem that a certain property is not available when it already should be and you ask someone at flexcoders you often [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>At my first posts I wrote about my favorite Flex feature: Databinding<br />
Now I will take a look at the opposite, IMHO the most precarious &#8220;feature&#8221; in Flex: <span class="hl">callLater()</span></p>
<p>If you have a dodgy problem that a certain property is not available when it already should be and you ask someone at <a href="http://tech.groups.yahoo.com/group/flexcoders/" target="_blank">flexcoders</a> you often get the advice, <em>&#8220;try callLater&#8221;</em> for a quick work-around. Sometimes this helps, but it leaves a bad smell, because often it´s just hiding some other problems in the code.<br />
So when we worked on a large-scale Flex application we have used it sometimes in the beginning, but after a while we decided to avoid callLater. To find and solve the real problem is simply the better solution &#8211; and we never needed it again &#8211; there was always another solution to solve a particular problem (maybe we just have had luck).</p>
<p>Unpleasantly we get confronted with the fact that callLater is used inside the Framework at the <strong>heart of the layout engine</strong>.<br />
We struggled with some strange problems. For instance we got a NullPointer exception in the updateDisplayList method of a custom component, which has already been cleaned up properly and removed from the displayList. It turned out that the layout mechanism delayed with callLater an invocation of updateDisplayList but our stuff there has already been removed and threw an exception.<br />
It was not hard to fix, but it demonstrated us that some asynchronous stuff was going on behind the scenes which was out of our direct control.</p>
<p>So for me callLater leaves always a certain bad smell.</p>
<p>Unfortunately I never found time to really investigate how it´s implemented and what are the concepts behind it.<br />
So it was time to <span class="hl">catch up</span> with this issue:<br />
CallLater is basically a method in UIComponent which delays a passed function to the next <strong>EnterFrame OR Render</strong> event.<br />
Often in den docs it´s just described that it will delay to the next frame, which is not correct because it could be that your function is executed already at the Render Event, which happens in the same frame and is the last opportunity where User code can be executed before the screen is rendered.</p>
<p>For more details about the internal concept of a frame in Flash and the relevant events see the great article by <a href="http://www.craftymind.com/2008/04/18/updated-elastic-racetrack-for-flash-9-and-avm2/" target="_blank">Sean Christmann</a>:</p>
<p>Here is a good illustration from his article about the <strong>anatomy of a frame</strong> in Flash:<br />
<span id="more-298"></span><br />
<img class="alignnone size-full wp-image-308" title="marshalledsliceexport" src="http://www.screenshot.at/blog/wp-content/uploads/2009/05/marshalledsliceexport.png" alt="marshalledsliceexport" width="500" height="220" /></p>
<p>All functions passed to callLater are queued up in an array and are executed at the EnterFrame event or Render event. It is possible that the executed function has some other callLater invocations again, so the final execution sequence could be delayed over several frames.</p>
<p>CallLater is used inside the Flex Framework in several classes. The main use case is probably inside the <strong>LayoutManager</strong>.<br />
There is a concept called <strong>phased instantiation</strong> which means basically that at application start-up the 3 phases in the UIComponents life-cycle (validate properties, validate size and validate displayList) are not executed in one pass, but are delayed with callLater to 3 half-frames. <strong>Half-frame</strong> because the Render event happens in the same frame as the EnterFrame event.<br />
This should reduce performance peaks at application-startup and should increase the UIs responsiveness.<br />
You can also use it later on in your application if you have to render heavy stuff, to balance the load across frames.</p>
<p><span class="hl">So far so good. But&#8230;</span><br />
First let me emphasis that I am sure that the SDK architects has had some good reasons for implementing callLater, but it <strong>introduces an asynchronism</strong> which could be hard to control. Normally with async stuff you have an event driven approach, so you get informed when the async task is done.<br />
With callLater it´s not so clear when the last callLater is really finished. There is an UpdateComplete event in UIComponent and LayoutManager which helps to get informed when the last callLater is executed, but in-between, you don´t have control about the distinct moments of your function calls.</p>
<p>Another issue is that you cannot control if your heavy load is really spread over 2 frames. It could be that it is just executed inside one frame at the EnterFrame event and at the Render event, so it could be that you don´t win anything from the load-balancing perspective.</p>
<p>Performance-wise there could also be perceptible delays caused by repeatedly callLater executions.</p>
<p>I guess there are many special situations in the framework code (probably mostly measurement stuff) which are difficult to handle, but i guess it <em>should</em> be possible (maybe I am too optimistic?).</p>
<p>There seems to be a problem to get the correct measurement when you set a new text in a TextField before the frame is actually rendered (see <a href="http://tech.groups.yahoo.com/group/flexcoders/message/121060" target="_blank"> this post</a> at flexcoders), but if this is an unsolvable problem then it should be urgently fixed in the Flash Player.</p>
<p>References@livedocs:<br />
<a href="http://livedocs.adobe.com/flex/3/langref/mx/core/UIComponent.html#callLater()" target="_blank">callLater()</a><br />
<a href="http://livedocs.adobe.com/flex/3/langref/mx/managers/LayoutManager.html#usePhasedInstantiation" target="_blank">usePhasedInstantiation</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.screenshot.at/blog/2009/05/05/the-infamous-calllater/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
