« Online survey for the first European 360Flex Conference | Main | Who is replacing the web? »

Techniques to clone an ActionScript 3 Object in Flex using AMF3

If you want to clone an Actionscript Object you have three possible ways: the deep-copy, the shallow-copy and the lazy-copy.

The deep-copy is the slowest but the most effective way, because the new object will lose any reference to the original object, so there will not be any data shared between the objects.


The shallow-copy results in a situation in which some data are shared between the objects, with the advantage that their execution is faster.

The lazy copy is a combination of both the above techniques. When initially copying an object, a (fast) shallow copy is used. A counter is also used to track how many objects share the data. When the program wants to modify an object, it can determine if data are shared (by examining the counter) and can do a deep copy if necessary.

The Flex API has a function that is called ObjectUtil.copy; it is a nice function but the bad thing is that you cannot work on all the object classes. You can only make your own function for deep copy, and to do that you need to know everything about its structure.

Here it is how Flex copies objects:

var buffer:ByteArray = new ByteArray();
buffer.writeObject(value);
buffer.position = 0;
var result:Object = buffer.readObject();
return result;

It works good, right?. Here is what it does: instead of introspection and recursively copying properties from one object to another it will serialize the object as an array of bytes and then wil de-serialize it.

If you use the AMF3 there can be some strange things going on: the failure may be caused by casting back to the original object class, and the [Transient] metadata tag can affect the output.

When the object will be de-serialized it will not be possible to creat it as an istant of a class (even if it has all its properties). If you want to have this you can inform AMF3 about the object class. There are 2 ways to do that:

use [RemoteClass] metadata
use registerClassAlias()

Even though the first way is better and nicer it may not work in all cases because the remote class information is stripped from the byte array in some cases.

Here is how to use the registerclass alias:

// TestClass doesn't have [RemoteClass] metadata or
// it does not work, so we need to make an association between
// the TestClass and the full qualified name (com.tests.TestClass)
registerClassAlias("com.tests.TestClass",TestClass);

// The result of the copy can be casted now without errors
var testCopy:TestClass = TestClass(ObjectUtil.copy(test));

Now, this is a very effective method but it has one flaw: it is not compatible with bitmapdata. Here is a workaround for this issue:

public function copyBitmap(target: DisplayObject) : Bitmap
{
// Create the bitmap data object with the right size.
var data : BitmapData = new BitmapData(target.width, target.height, true, 0);
// Draw the target object into the bitmap data.
data.draw(target);
// Create a new bitmap object associated with this data.
var bitmap: Bitmap = new Bitmap(data);
return bitmap;
}

Some interesting links where you can go deeper :
Object copy
ObectUtil.copy() Flex API reference

TrackBack

TrackBack URL for this entry:
http://blog.comtaste.com/mt-tb.cgi/31

Comments (3)

I just did a post about cloning in actionscript. option 2c, has a nice code example if you wanna take a look at it.

james:

Is this also applies on copying UI components? I am trying to copy a label from the first canvas of my viewstack to the second canvas. I tried using the "registeredClassAlias" to parse the object returned by ObjectUtil.copy to Label but it doesn't work. Any ideas?

Keystr0k:

Thanks SO much. I've been having the hardest time trying to use ObjectUtil.copy(bitmap); to copy bitmap data! I used the function you posted and now I can proceed with the project!

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on October 27, 2007 4:45 PM.

The previous post in this blog was Online survey for the first European 360Flex Conference.

The next post in this blog is Who is replacing the web?.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33