Wiki source code of EOF-Using EOF-Batch Fetching

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

Hide last authors
Pascal Robert 12.1 1 == Batch Fetching ==
Pascal Robert 4.1 2
Pascal Robert 12.1 3 === Petite Abeille ===
Pascal Robert 4.1 4
5 Here is a little example about how to batch fetch a bunch of EOFaults:
6
Quinton Dolan 10.1 7 * First, given an array of faults, how to retrieve meta information without triggering the fault:
Pascal Robert 4.1 8
Quinton Dolan 8.1 9 {{code}}
Pascal Robert 4.1 10
Quinton Dolan 8.1 11 EOEnterpriseObject aReferenceObject = (EOEnterpriseObject)someFaults.lastObject();
12 EOAccessFaultHandler aFaultHandler = (EOAccessFaultHandler) EOFaultHandler.handlerForFault(aReferenceObject);
13 EOEditingContext anEditingContext = aFaultHandler.editingContext();
14 String anEntityName = aFaultHandler.globalID().entityName();
Pascal Robert 12.1 15 EOEntity anEntity =
Quinton Dolan 8.1 16 EOModelGroup.defaultGroup().entityNamed( anEntityName );
Pascal Robert 4.1 17
Quinton Dolan 8.1 18 {{/code}}
Pascal Robert 4.1 19
20 * Second, for each EOFault build a qualifier:
21
Quinton Dolan 8.1 22 {{code}}
Pascal Robert 4.1 23
Quinton Dolan 8.1 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 );
Pascal Robert 4.1 28
Quinton Dolan 8.1 29 {{/code}}
Pascal Robert 4.1 30
31 * Finally, resolve the qualifier:
32
Quinton Dolan 8.1 33 {{code}}
Pascal Robert 4.1 34
Pascal Robert 12.1 35 EOQualifier aQualifier = new EOOrQualifier( someQualifiers );
Quinton Dolan 8.1 36 EOFetchSpecification aFetchSpecification = new EOFetchSpecification( anEntityName, aQualifier, null);
Pascal Robert 4.1 37 anEditingContext.objectsWithFetchSpecification?(aFetchSpecification?);
38
Quinton Dolan 8.1 39 {{/code}}
Pascal Robert 4.1 40
41 * Attached is the entire method.
42
Quinton Dolan 8.1 43 {{code}}
Pascal Robert 4.1 44
Quinton Dolan 8.1 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();
Pascal Robert 12.1 52
Quinton Dolan 8.1 53 for ( int index = 0; index < count; index++ ) {
54 Object anObject = someObjects.objectAtIndex(index);
Pascal Robert 12.1 55
Quinton Dolan 8.1 56 if ( EOFaultHandler.isFault( anObject ) == true ) {
57 someFaults.addObject( anObject );
58 }
59 }
Pascal Robert 12.1 60
Quinton Dolan 8.1 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 );
Pascal Robert 12.1 68
Quinton Dolan 8.1 69 count = someFaults.count();
Pascal Robert 12.1 70
Quinton Dolan 8.1 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 );
Pascal Robert 12.1 76
Quinton Dolan 8.1 77 someQualifiers.addObject( aQualifier );
78 }
Pascal Robert 12.1 79
Quinton Dolan 8.1 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 }
Pascal Robert 4.1 88
Quinton Dolan 8.1 89 {{/code}}
Pascal Robert 4.1 90
Quinton Dolan 8.1 91 === Jonathan Rochkind ===
Pascal Robert 4.1 92
Pascal Robert 12.1 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.
Pascal Robert 4.1 96
Quinton Dolan 8.1 97 == Preloading Relationships ==
Pascal Robert 4.1 98
Quinton Dolan 8.1 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.
Pascal Robert 4.1 100
Quinton Dolan 8.1 101 {{code}}
Pascal Robert 4.1 102
Quinton Dolan 8.1 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();
Pascal Robert 12.1 107
Quinton Dolan 8.1 108 EOObjectStoreCoordinator osc = (EOObjectStoreCoordinator) ec.rootObjectStore();
109 osc.lock();
110 try {
111 EOEntity entity = EOModelGroup.modelGroupForObjectStoreCoordinator(osc)
112 .entityNamed(sampleEO.entityName());
Pascal Robert 12.1 113
Quinton Dolan 8.1 114 EODatabaseContext databaseContext = EODatabaseContext.
115 registeredDatabaseContextForModel(entity.model(), osc);
Pascal Robert 12.1 116
Quinton Dolan 8.1 117 EORelationship relationship = entity.relationshipNamed(relationshipName);
Pascal Robert 12.1 118
Quinton Dolan 8.1 119 databaseContext.batchFetchRelationship(relationship, sourceEOs, ec);
120 } finally {
121 osc.unlock();
122 }
123 }
124 }
Pascal Robert 4.1 125
Quinton Dolan 8.1 126 {{/code}}