Re: sorting output
Re: sorting output
- Subject: Re: sorting output
- From: Nathan Dumar <email@hidden>
- Date: Fri, 20 Aug 2004 12:26:42 -0400
Okay, I understand better now. I'll explain amid parts of your code
below ...
public Main(WOContext context) {
super(context);
In here, code is executed once when the page is first loaded. If your
actions return null (when you make a new action in WOBuilder, instead
of selecting the page to follow, you select null), then this page will
be returned, but this code will not be executed again. It will just
pull the page from the page cache and return it to you. However, if,
when you create a new action, you select this page from the list
(instead of null), it will return a new object representing this page,
and the code here will be executed again. (At least, I'm pretty sure,
but not completely sure.)
Somebody correct me if I'm wrong (good possibility), but I *think* that
returning the page instead of returning null increases memory
requirements (which you probably don't have to worry about yet).
Returning null just gives what's already in memory.
// Sort ascending lastName ignoring case.
lastNameSort = new EOSortOrdering("lastName",
EOSortOrdering.CompareCaseInsensitiveAscending);
// Sort ascending by firstName ignoring case.
firstNameSort = new EOSortOrdering("firstName",
EOSortOrdering.CompareCaseInsensitiveAscending);
// Create an array with the two sort orderings.
sortOrderings = new NSMutableArray();
sortOrderings.addObject(lastNameSort);
sortOrderings.addObject(firstNameSort);
// Create the fetch specification.
fetchSpec = new EOFetchSpecification("Author", null, sortOrderings);
Good job on the fetch spec. Also check out the EOUtilities class for
some shortcuts in the most commonly performed fetches.
// Get the editing context.
editingContext = session().defaultEditingContext();
// Fetch authors.
authorList = new
NSMutableArray(editingContext.objectsWithFetchSpecification(fetchSpec))
;
As I mentioned, this fetch is done when the page first loads. Your
fetch spec tells the database to sort the objects before it returns
them, so it's the database, not WO, that is doing the sorting. Since
this code is not re-executed, the sort happens only at the beginning,
and not again until a new Main page object is created.
WebObjects keeps a copy of recently used objects in memory, so that it
can manipulate these instead of going back to the database, which takes
much longer. When you instruct it to do so, it will save all the
changes to all the objects, all at the same time, minimizing the
performance hit by doing everything all in the same trip.
If you want to return null from the action (the action that is bound to
the submit button of the form), but you still want to make WO fetch
from the database again, you can do a couple different things. You can
put the fetch within the method that is attached to the submit button
(forcing WO to go back to the database and get all the objects again),
or you can do an EOSortOrdering, which will re-sort the objects that it
has in memory, so they appear in the right order again.
// Get the Author class description from the Author Entity.
authorClassDescription =
EOClassDescription.classDescriptionForEntityName("Author");
// Create an Author object (where form data is stored).
author = new EOGenericRecord(authorClassDescription);
}
When is the main section run? Only at the beginning of the running
web app?
Yes, but if you have methods within it, and you call those methods,
they will run when called.
Would I have to set the fetchSpec in the "Add" method to get the
sorted list each time?
That depends. If the Add method does a saveChanges(), and you put the
fetch AFTER the save, then yes.
HTH,
Nathan
_______________________________________________
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.