Collection remove issue

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Collection remove issue

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:34 pm

by Jade Starter >> Mon, 5 Apr 2004 22:15:31 GMT

Hi,

I am running into a problem deleting certain references from a collection class. My collection class is called Games and it contains classes of type Game. In the collection class I created a method "deleteSome" which will remove some of the entries from the collection.

Here is the code I am using:

vars
game : Game;begin

foreach game in self do
if condition = true then
self.remove(game);
end if;
endforeach;
end;

When I ran it I got an error msg saying "entry not found in collection". This does not make any sense to me as I am traversing the collection and all entries have to exist. So I decided to take out the condition and try to remove all game instances from the collection. This gave me the same error.

I then tried to play around with the collection and unchecked "duplicates allowed". It required me to do a reorg. I ran it again and it worked. After further testing I noticed that doing a reorg fixes the problem.

Furtherone, I noticed that self.indexOf(loc) returns the correct number, but when I call self.includes(loc) it says false. So for some reasons it finds the entry sometimes, but not all the times.

Does anybody know why I am getting this error? Is there a way in my code to force a reorg before I execute the remove command?

Thanks for all your help in advance!
By the way, I am using Jade 6.0.16.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Collection remove issue

Postby ConvertFromOldNGs » Fri Aug 07, 2009 12:34 pm

by Rob Wilson >> Mon, 5 Apr 2004 23:01:46 GMT

Hi Jade Starter,

It's not a good idea to be removing objects from the membership of a collection that you are foreaching.

Instead, try gathering up the entries you want to remove into a new transient collection first, and then purge that collection once you are finished with the foreach. For example:

vars
gamesToDelete : Games ;
game : Game ;
begin

create gamesToDelete transient ;

foreach game in allGames do
if condition then
gamesToDelete.add( game ) ;
endif ;
endforeach ;

gamesToDelete.purge ; //deletes all the games in gamesToDelete in one go

epilog
delete gamesToDelete ; // don't forget to delete the transient collection when you are
// finished with it
end ;

You should also consider putting this method on the class that owns the collection rather than on the collection itself. (I've written the example method above as though that were the case, and the collection of games on that class is called allGames).

It sounds like you might also have an issue with the main collection being kept up to date as games are added and deleted. Have you created an inverse relationship between the class that has the collection of all the games and the Game class?

for example:
GameOwner::allGames inversed to Game::myGameOwner ?

Cheers,
Rob.


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 41 guests