cdugga
cdugga

Reputation: 3859

flex 3 webservice results issue

im having trouble with the result of a webservice call. When the result comes in and kicks off the resultHandler function i set a break point so i can examine the result. I can see that there are 0 records in the array collection however i can see content so im assuming that the zero is just referring to the first index of the array

the problem happens when i try assign the value to an array collection as follows;

public function resultHandler(event:ResultEvent):void{
    var result:ArrayCollection = event.result as ArrayCollection;

the result of this operation is a result var with the value of null. Can anyone explain what could be happening here? thanks a lot

another thing i just noticed is that the result type is mx.utils.ObjectProxy, im expecting an array

Upvotes: 1

Views: 4099

Answers (4)

paolo anghileri
paolo anghileri

Reputation: 1

The problem in my opinion is that you cannot cast event.result as an array collection, but you have to cast it as an array.

The best practice in this is having a getter and a setter:

private var _acLocation:ArrayCollection=new ArrayCollection;

public function set acLocation(acLocation:ArrayCollection):void{

_acLocation=acLocation;

//do this if you want for exaple to assign the arraycollection to a datagrid dataprovider

dgMyDataGrid.dataProvider=_acLocation;

}

public function get acLocation():ArrayCollection{


return _acLocation;
}

Then in the result handler function of a service call, code

acLocation=new ArrayCollection(event.result as Array);

Hope it helps

Upvotes: 0

Christian Nunciato
Christian Nunciato

Reputation: 10409

Chetan is right -- the cast operation to ArrayCollection is failing, because the source object is not an ArrayCollection. Try this instead:

public function resultHandler(event:ResultEvent):void
{
    var ac:ArrayCollection = new ArrayCollection([event.result])
    // ...
}

The "as" operator will return null in situations where an exception would occur at runtime -- in your case, casting from ObjectProxy to ArrayCollection. If instead you pass event.result as the sole member of an array (by surrounding it with []), your ArrayCollection will be constructed properly, and you'll be able to retrieve the object normally:

var o:Object = ac.getItemAt(0) as Object;
trace(o.yourObjectProperty.toString());

Hope it helps!

Upvotes: 1

Ryan Guill
Ryan Guill

Reputation: 13886

0 records in the array is the length of the array, that actually means 0. If you have something in index 0 of an array, that array has a length of at least 1. It looks like you aren't getting any data back, not even and empty array collection.

Upvotes: 1

Chetan S
Chetan S

Reputation: 23803

If the webservice returns just one element, it will be deserialized as ObjectProxy. You will have to manually convert it into an array.

I'd normally do this after a WS call:

if (event.result is ArrayCollection) {
    result = event.result;
}
else {
    result = new ArrayCollection([event.result]);
}

Upvotes: 2

Related Questions