Search code examples
apache-flexactionscriptairremoteobject

RemoteObject class aliases within ActionScript Worker


I have a Flex mobile app for iOS and Android. For performance reasons, I would like to move some of my download code into a Worker. I have about 20 Java classes which are being deserialized into ActionScript after making a RemoteObject call to download the data from a server. Each of those classes has a [RemoteClass(alias="com.mycompany.MyClass")] metadata tag on it. When in my main application, this all works great. In the Worker however, it does not have the class aliases registered for each of those classes. This means that when I get the data from the server, it is all generic Object classes instead of my own custom ActionScript classes. I was able to process the data correctly, only after manually re-registering those class aliases for all 20 classes within the Worker using registerClassAlias("com.mycompany.MyClass", MyClass);

I don't like that I have to maintain the aliases in two different places now. Is there any way to maintain the class aliases between my main swf and my Worker?


Solution

  • This could happen not because of Worker forgets about metadata, but because a Worker is a separate application in terms of AS3. Compiler throws away all your classes that are not used explicitly.

    So you have two options:

    1) Use classes explicitly in a Worker

    public class Worker extends ... {
        private static const FORCE_INCLUDE:Array = [MyClass1, MyClass2];
    }
    

    2) Use [ArrayElementType] metadata that forbides comiler to throw classes out

    public class Worker extends ... {
        [ArrayElementType("full.path.to.MyClass1")]
        [ArrayElementType("full.path.to.MyClass2")]
        public function Worker() { ... }
    }
    

    Also it could be possible to specifiy ArrayElementType directly on Worker class not on Worker's constructor.