Wiki source code of EOF-Using EOF-Batch Fetching

Last modified by Pascal Robert on 2007/09/03 13:54

Show last authors
1 == Batch Fetching ==
2
3 === Petite Abeille ===
4
5 Here is a little example about how to batch fetch a bunch of EOFaults:
6
7 * First, given an array of faults, how to retrieve meta information without triggering the fault:
8
9 {{code}}
10
11 EOEnterpriseObject aReferenceObject = (EOEnterpriseObject)someFaults.lastObject();
12 EOAccessFaultHandler aFaultHandler = (EOAccessFaultHandler) EOFaultHandler.handlerForFault(aReferenceObject);
13 EOEditingContext anEditingContext = aFaultHandler.editingContext();
14 String anEntityName = aFaultHandler.globalID().entityName();
15 EOEntity anEntity =
16 EOModelGroup.defaultGroup().entityNamed( anEntityName );
17
18 {{/code}}
19
20 * Second, for each EOFault build a qualifier:
21
22 {{code}}
23
24 EOEnterpriseObject aFault = (EOEnterpriseObject) someFaults.objectAtIndex(index);
25 EOKeyGlobalID aGlobalID = (EOKeyGlobalID) anEditingContext.globalIDForObject( aFault );
26 NSDictionary aPrimaryKey = anEntity.primaryKeyForGlobalID( aGlobalID );
27 EOQualifier aQualifier = anEntity.qualifierForPrimaryKey( aPrimaryKey );
28
29 {{/code}}
30
31 * Finally, resolve the qualifier:
32
33 {{code}}
34
35 EOQualifier aQualifier = new EOOrQualifier( someQualifiers );
36 EOFetchSpecification aFetchSpecification = new EOFetchSpecification( anEntityName, aQualifier, null);
37 anEditingContext.objectsWithFetchSpecification?(aFetchSpecification?);
38
39 {{/code}}
40
41 * Attached is the entire method.
42
43 {{code}}
44
45 // ===========================================================================
46 // Batch fetching Class method(s)
47 // ---------------------------------------------------------------------------
48 public static void batchFetchObjects(NSArray someObjects) {
49 if ( ( someObjects != null ) && ( someObjects.count() > 0 ) ) {
50 NSMutableArray someFaults = new NSMutableArray();
51 int count = someObjects.count();
52
53 for ( int index = 0; index < count; index++ ) {
54 Object anObject = someObjects.objectAtIndex(index);
55
56 if ( EOFaultHandler.isFault( anObject ) == true ) {
57 someFaults.addObject( anObject );
58 }
59 }
60
61 if ( someFaults.count() > 0 ) {
62 NSMutableArray someQualifiers = new NSMutableArray();
63 EOEnterpriseObject aReferenceObject = (EOEnterpriseObject) someFaults.lastObject();
64 EOAccessFaultHandler aFaultHandler = (EOAccessFaultHandler) EOFaultHandler.handlerForFault(aReferenceObject);
65 EOEditingContext anEditingContext = aFaultHandler.editingContext();
66 String anEntityName = aFaultHandler.globalID().entityName();
67 EOEntity anEntity = EOModelGroup.defaultGroup().entityNamed( anEntityName );
68
69 count = someFaults.count();
70
71 for ( int index = 0; index < count; index++ ) {
72 EOEnterpriseObject aFault = (EOEnterpriseObject) someFaults.objectAtIndex(index);
73 EOKeyGlobalID aGlobalID = (EOKeyGlobalID) anEditingContext.globalIDForObject( aFault );
74 NSDictionary aPrimaryKey = anEntity.primaryKeyForGlobalID( aGlobalID );
75 EOQualifier aQualifier = anEntity.qualifierForPrimaryKey( aPrimaryKey );
76
77 someQualifiers.addObject( aQualifier );
78 }
79
80 if ( someQualifiers.count() > 0 ) {
81 EOQualifier aQualifier = new EOOrQualifier( someQualifiers );
82 EOFetchSpecification aFetchSpecification = new EOFetchSpecification( anEntityName, aQualifier, null);
83 anEditingContext.objectsWithFetchSpecification(aFetchSpecification);
84 }
85 }
86 }
87 }
88
89 {{/code}}
90
91 === Jonathan Rochkind ===
92
93 Can you explain the advantages of fetching faults instead of just letting the editingContext return them? -SamBarnum
94 A 'fault' will not actually be fetched until it is asked for. It's just an object in memory that says 'if anyone tries to access my data, I will fetch it from the db.' You usually don't run into a situation where you want to create faults instead of just fetching objects from the EC~-~--but when you're doing complicated programming, it will be obvious when you do. I think just once have I actually manually created faults. [WO:When I had a semantic relationship, but couldn't actually model the relationship, and implemented a sort of half baked relationship scheme of my own instead.]
95 However, I'm not sure why you'd want to use the above code when the EOEditingContext method faultForRawRow is available. Make an NSDictionary which has pk attribute names as keys and desired values as their values, pass it to yourEC.faultForRawRow?, you've got a fault. A heck of a lot easier than the above.
96
97 == Preloading Relationships ==
98
99 This method can be used to prefetch relationships in a batch instead of having them fire one by one. This can be used to fine tune batch fetching in code instead of doing it in the model. Be careful of how you use this method. It will force a fetch from the database even if the objects are already cached in the object store. Used incorrectly this can reduce, instead of increase, performance.
100
101 {{code}}
102
103 public static void preloadRelationship(NSArray sourceEOs, String relationshipName) {
104 if (sourceEOs.count() != 0) {
105 EOEnterpriseObject sampleEO = (EOEnterpriseObject) sourceEOs.objectAtIndex(0);
106 EOEditingContext ec = sampleEO.editingContext();
107
108 EOObjectStoreCoordinator osc = (EOObjectStoreCoordinator) ec.rootObjectStore();
109 osc.lock();
110 try {
111 EOEntity entity = EOModelGroup.modelGroupForObjectStoreCoordinator(osc)
112 .entityNamed(sampleEO.entityName());
113
114 EODatabaseContext databaseContext = EODatabaseContext.
115 registeredDatabaseContextForModel(entity.model(), osc);
116
117 EORelationship relationship = entity.relationshipNamed(relationshipName);
118
119 databaseContext.batchFetchRelationship(relationship, sourceEOs, ec);
120 } finally {
121 osc.unlock();
122 }
123 }
124 }
125
126 {{/code}}