Version 43.1 by Pascal Robert on 2012/07/22 07:27

Show last authors
1 Some things to avoid when working with EOF. Some of these things are contraindicated in Apple documentation, others are not. But all are things that experience shows EOF does not expect, and can lead to all sorts of trouble, including mysterious exceptions, and EOF getting confused about what changes must be saved to the database.
2
3 1. h5. Don't use an EO's Constructor.
4 Don't set EO properties in the EO constructor - use ##awakeFromInsertion(...)## or ##awakeFromFetch(...)## instead.
5 From [[Apple's Documentation>>JavaDoc and other documentation^EnterpriseObjects.pdf]]:
6
7 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.
8 \\\\\\\\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.
9
10 When overriding ##awakeFromInsertion(...)## there are two times that this method can be called when the EO being inserted is not actually being initialized for the first time.
11
12 1.
13 11. If it's being reinserted to a EC after it was deleted from another or the same EC. Only new EOs will have a temporary GlobalID.
14 11. If it's being saved to the server-side application by a Java Client WebObjects application. These EOs will have been inserted on the client-side and likely already have values set. To avoid overriding these values, you should verify that the attribute's value is null first.
15
16 {{code}}
17
18 @Override
19 public void awakeFromInsertion(EOEditingContext ec) {
20 super.awakeFromInsertion(ec);
21 EOGlobalID gid = ec.globalIDForObject(this);
22 if(gid.isTemporary()) {
23 // only set default values when we're inserted into an EC for the first time.
24 if(createdDate() == null) {
25 // only set value if it hasn't already been set by a Java Client applciation.
26 setCreatedDate(new NSTimestamp());
27 }
28 }
29 }
30
31 {{/code}}
32
33 1. h5. Don't change the value of an attribute or behavior of a relationship in its accessor method
34 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. Don't:
35 1*. {{color value="#ff0000"}}{*}Don't{*}{{/color}} provide default values -
36
37 {{color value="#008000"}}
38 {*}Do{*}
39 {{/color}}
40
41 use ##awakeFromInsertion(...)## or ##awakeFromFetch(...)##
42
43 1.
44 1*. {{color value="#ff0000"}}{*}Don't{*}{{/color}} sort a to-many relationship's values -
45
46 {{color value="#008000"}}
47 {*}Do{*}
48 {{/color}}
49
50 write a convenience method like ##relatedObjectsSorted()##
51
52 1.
53 1*. {{color value="#ff0000"}}{*}Don't{*}{{/color}} format a String or Date or Number attribute -
54
55 {{color value="#008000"}}
56 {*}Do{*}
57 {{/color}}
58
59 formatting in helper classes or in the Component
60
61 1.
62 1*. {{color value="#ff0000"}}{*}Don't{*}{{/color}} change an attribute's data type
63 1*. {{color value="#ff0000"}}{*}Don't{*}{{/color}} change an attribute's value when reading a different attribute. For example: Don't call ##setFullName(...)## when you call ##firstName()##
64 1. h5. Always Insert First.
65 Don't do anything to an EO before inserting it into an editing context. Always insert EOs into ECs immediately. See rule #1.
66 1. h5. Don't modify any EO properties in ##validateFor...(...)## methods
67 Doing this in ##validateValueForKey(...)## is ok as Chuck Hill noted in the list.
68 1. h5. Always invoke ##super## when overriding EOF methods
69 If overriding ##awakeFromInsertion(...)##, remember to call the superclass implementation. Same with ##awakeFromFetch(...)##.
70 1. h5. Don't use EOF in model class static initializers
71 Doing so forces EOF into operational mode before the frameworks are initialized. Use lazy loading of the entity instead.
72 1. h5. Don't use mutable classes as attributes
73 Attributes of (##NSMutableArray##, ##NSMutableDictionary## or any other class that can change internal state after creation) will confuse EOF. If you want this effect, use immutable classes and provide cover methods to replace the immutable instance with an updated instance. For example:
74
75 {{code}}
76
77 public void addAppointment(String time, String reason) {
78 super.willChange();
79 NSMutableDictionary mutableAppointments = appointments().mutableClone();
80 mutableAppointments.setObjectForKey(reason, time);
81 setAppointments(mutableAppointments.immutableClone());
82 }
83
84 {{/code}}
85
86 1. h5. Always Lock your EditingContext
87 Don't call any EOEditingContext methods, or any methods on an EO in an EOEditingContext without first ensuring that the EOEditingContext is locked. That includes bindings in a WOD. It's your job to make sure the EO's that you bind to in a page are locked.
88
89 {{tip title="Project Wonder"}}
90 [Project Wonder|WONDER:] provides auto-locking, so you don't have to worry about this.
91 {{/tip}}
92
93 1. h5. Don't Expose Foreign Keys as Class Attributes
94 Doing this will result in buggy unexpected behavior in EOF. Same goes for compound PKs (Primary Keys) where the compound PK values are set automatically by EOF (for example in a many-to-many join entity). However exposing compound PK attributes as class attributes is OK for an entity where you are manually setting the PK values yourself. Interestingly, even though it is not recommended, exposing a Primary Key as a class attribute will not break EOF.
95 1. h5. Always use .imutableClone() when iterating over a to-many relationship with a for-each loop.
96 It doesn't matter if you use the relationship directly or use a local variable that was populated by ##myRelatedObjects()## or ##getValueForKey("myRelatedObjects")## you should always use an imutableClone of the array, otherwise you will likely just have a pointer to the actual relationship, which can easily change.