Re: FYI: Possible buggy EOGenerator code?
Re: FYI: Possible buggy EOGenerator code?
- Subject: Re: FYI: Possible buggy EOGenerator code?
- From: Chuck Hill <email@hidden>
- Date: Fri, 31 Dec 2004 10:57:41 -0800
At 11:41 PM 30/12/2004 -0500, Kieran Kelleher wrote:
>The thing is that the code was actually removing the relationships one
>at a time, not modifying the objects themselves
Perhaps you are confused. Of course removing the relationship modifies the
object.
removeObjectFromBothSidesOfRelationshipWithKey(object, "regions");
Is going to result in this call on this object (and a related one on object.
regions.removeObject(object);
>..... seemed unusual that the fixed list
Fixed list? What fixed list? There is one list, it is an NSMutableArray
and most certainly not fixed, and you are copying references to it.
regions() returns a reference to the list of regions, not an immutable copy
of it. The correct implementation of the method is to make a fixed copy to
enumerate:
public void deleteAllRegionsRelationships() {
Enumeration objects = new NSArray(regions()).objectEnumerator();
while ( objects.hasMoreElements() )
deleteRegionsRelationship((CTRegion)objects.nextElement());
}
>of region EOs originally returned would change due
>to removing from both sides of relationship since the region EOs
>themselves were not being deleted.
>
But regions is not on Region, it is on this. If the list of regions did
not change, then the objects would not have been removed from the
relationship, no?
Chuck
>Regards, Kieran
>
>On Dec 30, 2004, at 5:07 PM, Chuck Hill wrote:
>
>> Never alter a collection that you are enumerating. It causes undefined
>> results. NSEnumerator used to document this explicitly. This is one
>> the
>> reasons (I think) that Iterator was added to Java:
>>
>> Iterators allow the caller to remove elements from the underlying
>> collection during the iteration with well-defined semantics.
>>
>> Chuck
>>
>>
>> At 04:46 PM 30/12/2004 -0500, Kieran Kelleher wrote:
>>>>>>
>> I have spent hours on this and I suspect (I might be wrong ... if I am
>> correct, then this is important to all) there is a bug in section of
>> code
>> that's generated by the the typical EOGenerator base class template. I
>> have changed the code to solve my problem, but I am sending this as an
>> FYI
>> in case you are affected and did not realize it.
>>
>> If someone else has a chance to verify that I am correct, then please
>> let
>> me know.
>>
>> A common utility method for deleting all objects in a to-many
>> relationship
>> and the default code generated by any of the EOGenerator templates I
>> have
>> come across looks like this (example of an object with to-many region
>> objects):
>>
>> public void deleteRegionsRelationship(CTRegion object) {
>> removeObjectFromBothSidesOfRelationshipWithKey(object,
>> "regions");
>> editingContext().deleteObject(object);
>> }
>>
>> public void deleteAllRegionsRelationships() {
>> Enumeration objects = regions().objectEnumerator();
>> while ( objects.hasMoreElements() )
>> deleteRegionsRelationship((CTRegion)objects.nextElement());
>> }
>>
>> The typical EOGen templates also have these similar patterned methods
>> for
>> removing the relationships only and not deleting:
>> public void removeFromRegionsRelationship(CTRegion object) {
>> if( log.isDebugEnabled() ) log.debug( "removing "+object+" from
>> regions relationship" );
>> removeObjectFromBothSidesOfRelationshipWithKey( object,
>> "regions" );
>> }
>>
>> public void removeAllFromRegionsRelationships() {
>> Enumeration objects = regions().objectEnumerator();
>> while ( objects.hasMoreElements() )
>> removeFromRegionsRelationship((CTRegion)objects.nextElement());
>> }
>>
>> These methods are in my _Geography.java base class where
>> geography <<-->> region (yes a many-to-many).
>>
>> My SPECIFIC problem today was with the removeAll... method. The
>> symptom was
>> that all the regions were NOT being removed from the relationship.
>> Logging
>> showed that after a few iterations, the enumeration began returning
>> nulls
>> resulting in some regions not being removed from the relationship.
>>
>> In troubleshooting, I broke the above code down into smaller parts
>> using
>> NSArray theRegions = regions() and logged out the contents of the
>> NSArray
>> during each iteration and to my surprise the NSArray was shrinking!
>>
>> The final solution turned out to require creating a new NSMutableArray
>> based on the regions() NSArray and enumerating through that as shown in
>> this working code that works properly.
>>
>> public void removeAllFromRegionsRelationships() {
>> NSMutableArray objects = new NSMutableArray( regions() );
>>
>> if (objects != null) {
>> Enumeration e = objects.objectEnumerator();
>> while ( e.hasMoreElements() )
>> removeFromRegionsRelationship((CTRegion)e.nextElement());
>> }
>> }
>>
>> Consequently, I _suspect_ that the deleteAll (which is common in the
>> EOGen
>> templates and follows the exact same code pattern) will suffer the same
>> problem. I don't know whether this affects to-many relationships that
>> are
>> _not_ part of a many-to-many type scenario ..... but I cannot spend
>> more
>> time today setting up test cases ...... if anyone is using EOGenerator
>> with
>> the standard deleteAll... method being used on a many-to-many
>> relationship,
>> and has time to test it, then please provide feedback.
>>
>> Regards, Kieran
>>
>> ________________________________________________________________
>> Dev Config = OS X 10.3.5 / Java 1.4.2_05 / WO 5.2.3 / XCode v1.5 /
>> MySQL
>> 4.0.20 / Connector-J 3.0.11
>> Deploy Config = OS X 10.3.5 Server / Java 1.4.2_05 / WO 5.2.3 / MySQL
>> 4.0.20 / Connector-J 3.0.11
>> My Blog: http://webobjects.webhop.org/
>>
>>
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> Webobjects-dev mailing list (email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> village
>> .net
>>
>> This email sent to email@hidden<<<<
>>
>>
>> --
>>
>> Chuck Hill email@hidden
>> Global Village Consulting Inc.
>> http://www.global-village.net
>>
>
>
--
Chuck Hill email@hidden
Global Village Consulting Inc. http://www.global-village.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden