Re: Most efficient way to iterate through an NSArray
Re: Most efficient way to iterate through an NSArray
- Subject: Re: Most efficient way to iterate through an NSArray
- From: David Avendasora <email@hidden>
- Date: Mon, 17 Mar 2008 12:55:11 -0400
I think I mis-spoke. It doesn't return an array of faults. If the
relationship itself is a fault, simply calling products() without
the .toArray() in a for loop returns a fault and trying to iterate
over it doesn't work.
At least this was the case in 5.3. I haven't tested it in 5.4.
See this pervious thread from almost exactly a year ago:
http://www.mail-archive.com/email@hidden/msg09829.html
Dave
On Mar 17, 2008, at 9:48 AM, Alexander Spohr wrote:
Hey David.
Am 17.03.2008 um 11:41 schrieb David Avendasora:
I find myself often times needing to iterate through an NSArray of
EOs. I'm not sure if the way I'm doing it is the most effecient or
not. Here's what I normally do:
public void doThisForAllProducts(EOEditingContext ec) {
for (Object aProductObject : products().toArray()) {
Product aProduct = (Product) aProductObject;
aProduct.doThis(ec);
}
}
This is assuming that products() is an NSArray of Product EOs.
I'd drop that toArray() as it will not be faster in fetching your
faults than by iterating the NSArrays items. (Or does it automagicly
batchFetch?)
Is this a good / the best way to iterate through NSArrays in general?
It depends :)
I find myself occasionally nesting this loop a couple deep to drill
down into several toMany relationships. For example (as a keypath):
products.billsOfMaterial.routingSteps.components to get all the
components that are possibly used in a product.
If you know that you have fetched all your products before in this
edcon just run the loops, no faster way available.
The second fastest way to do that is to fetch only the final array
by building an OR-qualifier pointing to your products.
The third fastest way is to batchFetch every array before you iterate.
On a side note, and the reason for calling products().toArray() is
to force EOF to get the real EOs and not just faults. If I just
call products() I'll sometimes get EOs, sometimes faults, seemingly
by EOF whim. Is that a bug or expected behaviour?
If a product was fetched before it will not be a fault any more. But
why do you care at all if they are faults? Just call doThis() and
EOF will get them for you.
atze
ps. giving doThis an edcon makes me shiver. aProduct already knows
its edcon and should not be allowed to work on a foreign one.
_______________________________________________
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