!
©!2009!DevelopmentArc!LLC,!All!rights!reserved.!
In!our!example,!we!have!a!component!that!needs!a!property!called!dataValue,!
which!is!an!Array!of!Numbers.!!When!our!dataValue!property!changes!we!first!need!
to!loop!over!the!Array!and!calculate!the!total!value.!Once!the!total!value!is!updated,!
we!need!to!draw!the!values,!as!a!chart,!to!the!screen!using!the!drawing!API.!
Without!knowing!about!the!Invalidation‐Validation!cycle!the!developer!may!write!
code!that!looks!something!like!this:!
[Bindable] public var total:Number;
private var __dataValue:Array;
public function get dataValue():Array {
return __dataValue;
}
public function set dataValue(value:Array):void {
__dataValue = value;
// calculate the total value
var len:int = value.length;
total = 0; // reset total
for(var i:uint=0; i < len; i++) {
total += Number(value[i]);
}
// draw the data to screen
drawChart();
}
Figure'12')'Un)opt imized'Code'
Fundamentally,!the!code!in!Figure!12!‐!Un‐optimized!Code#is!okay,!but!we!execute!
this!potentially!heavy!process!every!time!the!value!changes.!!Looking!back!at!the!
AVM2!Marshal!in!The!Elastic!Racetrack#section,!our!dataValue!may!change!multiple!
times!before!our!next!screen!render!occurs.!!!
This!means!that!we!are!both!updating!and!calculating!the!total!each!time!it!
changes,!which!also!kicks!off!binding!events!because!we!are!changing!the!total’s!
value.!!We!are!also!calling!our!drawChart()!method!which!uses!the!drawing!API!to!
update!the!UI!display.!!Since!we!may!not!have!executed!a!screen!draw!between!each!
value!change,!we!are!needlessly!using!the!drawing!API!and!using!unneeded!
processor!cycles.!
So!how!do!we!solve!the!issues!raised!in!Figure!12!‐!Un‐optimized!Code?!We!need!to!
do!two!things,!first!prepare!for!the!fact!that!the!dataValue!may!change!multiple!
times!before!we!are!ready!to!render!to!screen.!!Second,!we!need!to!move!our!
calculations!and!drawing!out!of!the!setter!method!and!into!the!Validation!methods!
so!that!they!are!executed!only!when!the!values!have!changed.!
Commenti su questo manuale