Re: Caching problem?
Re: Caching problem?
- Subject: Re: Caching problem?
- From: David Griffith <email@hidden>
- Date: Tue, 02 Mar 2004 19:01:55 +0100
Thanks Jahnathan,
You're right, I didn't realise that this.productPrices() actually set up a
reference, I thought it made a copy. That's more or less what I guessed was
happening, but I couldn't see where. Now I see :-)
I'll try your suggestions and see what happens!
Dave.
> Well, when you run EOQualifier.filterArrayWithQualifier, you are
> filtering prices out of the array passed in as an argument. You are
> _changing_ the array passed in as an argument. Well, what's the array
> passed in as an argument? If we look at your code, it's the actual
> this.productPrices() array, the actual array EOF is using to store
> the relationship destination! That's not what you meant to do, as
> evidenced by these lines of somewhat confused code:
>
> //First, we initialize the productPrices variable to a brand new array
>> NSMutableArray productPrices = new NSMutableArray();
>
> //But then, we set the productPrices variable to point to
> this.productPrices()!
> //Why did we even bother initializing it to a brand new array above then, if
> //we were immediately going to forget about the brand new array?
> productPrices = (NSMutableArray)this.productPrices();
>
>
> So you've got the productPrices local variable pointing to the _same_
> array as this.productPrices(), and when you filterArrayWithQualifier,
> you are removing objects from this array, the EOF array tracking the
> relationship. That's always a a bad idea. Perhaps what you want
> instead isL
>
> NSMutableArray productPrices = this.productPrices().mutableClone();
> //mutableClone() says give me a _copy_ of the array, another seperate
> array holding
> //the same elements.
>
> Or, perhaps, instead of EOQualifier.filterArrayWithQualifier, which
> filters the array in place and removes elements from teh array passed
> in, you want:
> EOQualifier.filteredArrayWithQualifier
> Which leaves the original array untouched, but creates a _new_ array
> filtered by the qualifier, and returns it.
>
> You may run into issues of EOF snapshoting and "caching", which can
> be trickier to diagnose--but that's not what you're dealing with
> here. Your problem here is that that method has a side-effect, it
> actually removes objects from the this.productPrices() array. Which
> you never want to do, you never want to mess with the actual internal
> array tracking a relationship like this.
>
> --Jonathan
>
> At 6:23 PM +0100 3/2/04, David Griffith wrote:
>> Hi,
>>
>> I'm not so sure how the caching works and I'm wondering if this might be a
>> caching issue.
>>
>> I have a Product table with a relationship to ProductPrice called
>> productPrices. This is basically a many-to-many relationship based on a
>> Country.
>>
>> In Product.java is a method called productPrices() which returns an NSArray
>> of the product prices for this product. Each product price has a link to a
>> Country also, so I want to filter the productPrices for a certain country.
>>
>> This works fine with the following:
>>
>> public ProductPrice getProductPriceForCountry(Country country) {
>> NSMutableArray productPrices = new NSMutableArray();
>> productPrices = (NSMutableArray)this.productPrices();
>> NSMutableArray bindings = new NSMutableArray();
>> bindings.addObject(country);
>> EOQualifier countryQualifier =
>> EOQualifier.qualifierWithQualifierFormat("country = %@", bindings);
>> EOQualifier.filterArrayWithQualifier(productPrices,countryQualifier);
>> return (ProductPrice)productPrices.objectAtIndex(0);
>> }
>>
>> However, it works for the FIRST time, if I change the country, then the
>> product prices that I get back from productPrices() only gives me the
>> filtered country from the previous time, not all the prices.
>>
>>
>> Could the filter still somehow be active? If so, can I remove it?
>>
>> Dave.
>> _______________________________________________
>> webobjects-dev mailing list | email@hidden
>> Help/Unsubscribe/Archives:
>> http://www.lists.apple.com/mailman/listinfo/webobjects-dev
>> Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.