Question

I’m new to Flex and Cairngorm.While I’m using ServiceLocator,I do run into the problem: Error: C0001E: Only one ServiceLocator instance can be instantiated.

My Code is like this:

In Serives.mxml:

<cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cairngorm="com.adobe.cairngorm.business.*">
<mx:HTTPService id="statistServ"
    url="rooms.xml"
    showBusyCursor="true"
    method="POST"
    resultFormat="array"/>

In Delegate.as,I have snippets:

this.service = ServiceLocator.getInstance().getHTTPService(”statistServ”);

In Main.xml,snippets like:

<business:Service id="service" />

this wonderful little Error message pops up the minute I load a second instance of some module which requires httpservice.

Is there any way to resolve this problem without switching to another Framework?

Best Wishes,

Shuo from China

Was it helpful?

Solution

The error you're seeing is from Cairngorm's implementation of the Singleton pattern. It's preventing you from creating a second instance of ServiceLocator because the framework requires that there be only one. When loading the second instance of your module, your code is also trying to create a second instance of ServiceLocator when it should be using getInstance() (or not getting an instance at all).

By declaring your Services tag in Main.mxml, you've created your lone instance of ServiceLocator and it cannot be declared or newed anywhere else. If Main.mxml is part of the module that you're trying to instantiate more than once, that's your problem right there. If not, I can't tell where in your code the problem is based on the limited sample above, but it should be somewhere in the module.

In reply to comment:

No problem. You can easily solve your problem by declaring your Service in ActionScript instead of MXML. By declaring a tag in MXML, you're always creating an instance of the corresponding AS class, whereas you instead want to retrieve the lone instance of the singleton. To do this, include the following AS code inside of your module MXML:

var service:Service = Service(ServiceLocator.getInstance());

That is essentially equivalent of what you're trying to do with the MXML tag, with the exception of not creating a new Service instance.

However, you still need to instantiate the Service somewhere. To do that, put the Service MXML tag inside of the main MXML of the application which is loading the modules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top