On Apr 5, 2007, at 12:02 PM, Chuck Hill wrote:

   public WOActionResults postsAction() {
   WOComponent nextPage = pageWithName("TopicPosts");
Number topicID = request().numericFormValueForKey("topicID", new NSNumberFormatter()); EOEnterpriseObject topic = EOUtilities.objectWithPrimaryKeyValue(WOApplication.application ().sharedEditingContext(), "Topic", topicID);

That rather looks like an abuse of sharedEditingContext().

That it does! According to the EOSharedEditingContext docs, only 4 EOSharedEditingContext methods that make changes to the shared editing context are thread-safe. We don't know whether EOUtilities.objectWithPrimaryKeyValue() uses one of these thread-safe methods. If it doesn't, the shared editing context must be locked prior to its use, something that can be difficult to do correctly.

Instead, fetch all shared objects that an app instance will need in the Application constructor using one of the thread-safe methods. Then access these shared objects by sending the shared editing context either an objectsByEntityName() or objectsByEntityNameAndFetchSpecificationName() message, getting the array of objects for the particular entity (e.g., Topic) from the returned dictionaries, and then filtering this array, if necessary, to get the object(s) desired.

Also, exposing the Topic primary key, normally a meaningless integer, and then using it to get a Topic object isn't the pattern usually used for the Enterprise Objects Framework (EOF). Instead, an attribute whose value is unique and meaningful to humans is a better choice for accessing an object. E.g., topicName, if such attribute exists.

Number topicID = request().numericFormValueForKey("topicID", new NSNumberFormatter()); EOEnterpriseObject topic = EOUtilities.objectWithPrimaryKeyValue(WOApplication.application ().sharedEditingContext(), "Topic", topicID);

    EOEnterpriseObject topic;
    String topicName = request().formValueForKey("topicName");

    if (topicName != null) {
NSArray topics = WOApplication.application ().sharedEditingContext().objectsByEntityName().objectForKey("Topic"); EOQualifier qual = new EOKeyValueQualifier("topicName", EOQualifier.QualifierOperatorLike, topicName); topic = (EOEnterpriseObject) EOQualifier.filteredArrayWithQualifier(topics, qual).lastObject();
    }
    else {
        topic = null;
    }
    if (topic == null) {
        // Deal with no Topic object.
    }

While this may appear more verbose, EOUtilities.objectWithPrimaryKeyValue() can throw 3 exceptions, so it should be in a "try" block with "catch" blocks dealing at least one of these exceptions. Once you've done all

Number index = request().numericFormValueForKey ("displayBatchIndex", new NSNumberFormatter());
   // set variables
nextPage.takeValueForKeyPath(topic, "postsDisplayGroup.masterObject"); if (index != null) nextPage.takeValueForKeyPath(index, "postsDisplayGroup.currentBatchIndex");
   return nextPage;

Also, as others have warned, using KVC to set and get values increases the probability of run-time exceptions should you mistype a key or key path. Such errors won't be caught by the compiler. Instead, send actual Java messages. Any mistyping of these messages will be caught at compile time rather than at run time which is much- preferred :-)

    WODisplayGroup postsDG = nextPage.postsDisplayGroup();
    postsDG.setMasterObject(topic);
    if (index != null) postsDG.setCurrentBatchIndex(index);

Aloha,
Art

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to