Child pages
  • EOF-Using EOF-The EOF Commandments

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Don't set EO properties in the EO constructor - use awakeFromInsertion(...) or awakeFromFetch(...) instead.
    Note: When overriding awakeFromInsertion(...), it's important to check that the EO isn't being reinserted to the EC after it was deleted. For example:
    Code Block
    @Override
    public void awakeFromInsertion(EOEditingContext ec) {
        super.awakeFromInsertion(ec);
        EOGlobalID gid = ec.globalIDForObject(this);
        if(gid.isTemporary()){
            // only set default values when we're inserted into an EC for the first time.
            // For example:
            //	takeValueForKey(new NSTimestamp(), CREATED_DATE_KEY);  // using static name generated by EOGeneration
            //	setCreatedDate(new NSTimestamp());  // using set method
        }
    }
    
  2. Don't do anything to an EO before inserting it into an editing context. Always insert EOs into ECs immediately. See rule #1.
  3. Don't modify any EO properties in validateFor...(...) methods. Doing this in validateValueForKey(...) is ok as Chuck Hill noted in the list.
  4. If overriding awakeFromInsertion(...), remember to call ther the superclass implementation. Same with awakeFromFetch(...).
  5. Don't change the behavior of methods that EOF uses. For example, do not override to-many relationships to return a sorted list of the related objects. Make another method to do this.
  6. Don't use EOF in model class static initializers. Doing so forces EOF into operational mode before the frameworks are initialized. Use lazy loading of the entity instead.
  7. Don't use mutable classes (NSMutableArray, NSMutableDictionary, or any other class that can change internal state after creation) as attributes. If you want this effect, use immutable classes and provide cover methods to replace the immutable instance with an updated instance. You and EOF will be much, much, much happier. For example:
    Code Block
    public void addAppointment(String time, String reason) {
        super.willChange();
        NSMutableDictionary mutableAppointments = appointments().mutableClone();
        mutableAppointments.setObjectForKey(reason, time);
        setAppointments(mutableAppointments.immutableClone());
    }
    
  8. Don't change the value of an attribute in the accessor method! EOF calls those accessor methods many times over the life of the EO. An accessor method is the last place that you want to be making any changes or doing any computations to determine the value. Default values should be set in awakeFromInsertion(...). If you need to update one attribute (i.e. lastChangedDate) when another attribute (i.e. userName) is changed then implement the setUserName() method and document that it has the side effect of calling setLastChangedDate(...). An alternate method if you would need to implement too many set methods or if you're concerned about overhead of calling setLastChangedDate() too many times is to implement a listener for the editingContextWillSaveChanges notification from the EOEditingContext and call setLastChangedDate(...) from there.