Expand my Community achievements bar.

Adding multiple items at once to a SharedCollection

Avatar

Level 1
I'm playing around with Cocomo for the first time. I like it
a lot, but haven't yet got enough understanding of the API. So I'm
sure I'm missing something obvious.



Anyway. I want to build an application that share an array of
items between clients. The array should be persistent and when a
new client connects it will get the latest data. At the startup of
the first client I want to fill the shared array with default data.
I also want to revert to this data from time to time in an
applications lifecycle.



I've been checking out the examples and I think a
SharedCollection would be perfect for this scenario (not sure if it
really is?).



The problem I have is when I try to fill the SharedCollection
with data, using the addItem method within a for loop, I only get
an entry with the last item, instead of multiple entries (I'm
checking what data that has been added by using the Dev Console).
I'm doing all this in the SYNCHRONIZATION_CHANGE event (only the
first time it's fired).



I should also note that this happens the first time, when no
CollectionNode already is created. When a CollectionNode already
exist it works.



Is this totally the wrong way to go? Am I missing something
about how CollectionNodes should be created? Any help is highly
appriciated!
6 Replies

Avatar

Former Community Member
Hi ,

I actually modified the SharedCollectionExample locally to
have items added in the beginning in synchrinization change
handler. It works fine for me , here is what I did

In the SharedCollectionExample in the examples folder ,

add this line
_model.addEventListener(CollectionNodeEvent.SYNCHRONIZATION_CHANGE,onSynchronizationChange);


in the onCreationComplete() method where you first declare
SharedCollection Class.

And in the

private function
onSynchronizationChange(p_evt:CollectionNodeEvent):void

{

if ( _model.isSynchronized ) {

_model.addItem({State:"Nevada",Capital:"Carson City"});

_model.addItem({State:"California",Capital:"Sacramento"});

}

}



I can get multiple items( i.e. these two items i added) in
the very beginning. If you check that the model ( SharedCollection)
is synchronized and add within it the items it should work fine and
will give you an initial set of items. Also, shared collection has
a property called idField and it will index objects based on this
field value with duplicates being overwritten.



I hope this helps. Otherwise please copy fractions of code
where you are still finding problem.



Thanks

Hironmay Basu

Avatar

Level 1
Thanks for the answer!



I tried to update the SharedCollectionExample as you
suggested and it worked for me also. Then i tried to use the same
data items as in SharedCollectionExample in my app (State and
Capital) with State as idField (I had set the idField erlier also)
and then it worked!



Previously I used integers as id's for the items. It seems it
has to be String (haven't tried other datatypes).



So, I just converted my integers to Strings for the idField
and it worked perfect.



Thank you for your input!

Avatar

Level 1
Now that I got multiple addItem calls working, is there any
way to prevent events from being dispatched when adding items? I
have tried calling "disableAutoUpdate" before adding new items and
"enableAutoUpdate" when I'm done, but it doesn't seem to work.



Or do I just have to remove my COLLECTION_CHANGE event
listener?

Avatar

Former Community Member


Hi Nisse,



Could you provide a little more context as to what you're
trying to accomplish? Why is having events dispatched a bad thing?
In general, you shouldn't suppress events at the source - it's a
bit of a "tree falling in the woods" problem; even if no one wants
to listen, it should still dispatch an event =). I'd just remove
your COLLECTION_CHANGE listener.



cheers

nigel

Avatar

Level 1
Hi Nigel,



The reason why i don't want events is that I'm adding about
100 items in a for loop when setting my "default data". I'm setting
the items by calling the addItem method. When setting the data I'm
not interested in any events. I perfectly understand your reasoning
about "tree falling in the woods", so maybe the best solution is to
just remove my event listener when setting the data.



Or is there another way to set the data instead of adding it
via the addItem method?

Avatar

Former Community Member
Hi,



I think for your case, the best way will be to call
removeEventListener for COLLECTION_CHANGE before the for loop and
after the for loop, again call addEventListener for
COLLECTION_CHANGE. This event keeps getting fired for any change in
collection, including add, delete or edit.



Thanks

Hironmay Basu