Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Providing Initial Values

One of the most common business rules is to assign initial values to newly inserted records. For example, when a user creates a new record, you may want to immediately populate a field in that record called creationDate.

To do this, simply override the method awakeFromInsertion in an enterprise object class:

public void awakeFromInsertion(EOEditingContext context) {
    super.awakeFromInsertion(context);
    if (creationDate() == null) {
        setCreationDate(new NSTimestamp());
    }
}

This method sets the creationDate attribute to the current time. (Determining if the attribute is null is not usually necessary except in three-tier Java Client applications.)

Important: Always invoke super when overriding methods in Enterprise Objects.

awakeFromInsertion is automatically invoked immediately after an enterprise object class is instantiated and inserted into an editing context. You use this method to set default values in enterprise objects that represent new data.

For enterprise objects that represent existing data (data that’s already stored in a data source), you can use the method awakeFromFetch to provide additional initialization. This method is sent to an enterprise object that has just been created from a database row and initialized with database values.

You may wonder why it’s not recommended to initialize an enterprise object’s values in an object’s constructor. An enterprise object’s constructor represents the earliest state in the life of a particular object. The state of an enterprise object at the point of construction is not complete; the object is not fully initialized. It may not yet have been inserted into an editing context and it might not even have been assigned a global ID. You don’t ever want to manipulate an enterprise object that isn’t in an editing context or that doesn’t have a global ID.

Therefore, any logic you add in an enterprise object’s constructor may fail or be invalidated while the object finishes initializing. By providing custom logic in awakeFromInsertion or awakeFromFetch, however, you are guaranteed that an enterprise object is fully initialized.



< Previous PageNext Page > Hide TOC


Last updated: 2007-07-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice