Skip to content
This repository

Generate fetched results controllers for to-many relationships #88

Merged
merged 6 commits into from about 2 years ago

2 participants

Daniel Tull Jonathan 'Wolf' Rentzsch
Daniel Tull

This are generated when frc=true is used as a template variable.

The cache name is generated from a combination of the entity name, indexed attributes, relationship name and sort descriptors to try to make it unique.

If there are no indexed attributes, the cache name is nil because using different fetch requests with the same cache name completely borks the fetched results controller.

Daniel Tull Generate fetched results controllers for to-many relationships
This are generated when `frc=true` is used as a template variable.

The cache name is generated from a combination of the entity name, indexed attributes, relationship name and sort descriptors to try to make it unique.

If there are no indexed attributes, the cache name is nil because using different fetch requests with the same cache name completely borks the fetched results controller.
99ec596
Daniel Tull

Is there a way within the template to count the number of attributes that are indexed? This will allow the template to know whether it can generate a cache name or not rather than putting that logic in the generated code.

Jonathan 'Wolf' Rentzsch
Owner

Thanks man

Is there a way within the template to count the number of attributes that are indexed?

I don't think that's possible within the template, but would be easy to add a category in mogenerator.m that the template could read.

Let me know if you want to tackle the category or if you just want me to pull this right now.

Daniel Tull

I'll take a look into the category. :)

Daniel Tull

If everyone can look past my stupidity for a second there, this now produces nice clean output. :)

Jonathan 'Wolf' Rentzsch rentzsch merged commit c0a0ba4 into from January 25, 2012
Jonathan 'Wolf' Rentzsch rentzsch closed this January 25, 2012
Jonathan 'Wolf' Rentzsch
Owner

That's exactly what I was looking for, thanks! Pulled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 6 unique commits by 1 author.

Jan 24, 2012
Daniel Tull Generate fetched results controllers for to-many relationships
This are generated when `frc=true` is used as a template variable.

The cache name is generated from a combination of the entity name, indexed attributes, relationship name and sort descriptors to try to make it unique.

If there are no indexed attributes, the cache name is nil because using different fetch requests with the same cache name completely borks the fetched results controller.
99ec596
Jan 25, 2012
Daniel Tull Fix to test for not arc 6aff9fd
Daniel Tull Add key value coding for count on NSArray 9fa14f9
Daniel Tull Add indexedNoninheritedAttributes and use in the template to generate…
… cleaner output
499969e
Daniel Tull Revert "Add key value coding for count on NSArray"
This reverts commit 9fa14f961771ef408ed0ea29f7ab1b7c059f8830.
a228310
Daniel Tull Use @count to get the count of the array b2ce79b
This page is out of date. Refresh to see the latest.
5  mogenerator.m
@@ -114,6 +114,11 @@ - (NSArray*)noninheritedFetchedProperties {
114 114
 		return [[[self fetchedPropertiesByName] allValues]  sortedArrayUsingDescriptors:sortDescriptors];
115 115
 	}
116 116
 }
  117
+/** @TypeInfo NSAttributeDescription */
  118
+- (NSArray*)indexedNoninheritedAttributes {
  119
+	NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isIndexed == YES"];
  120
+	return [[self noninheritedAttributes] filteredArrayUsingPredicate:predicate];
  121
+}
117 122
 
118 123
 #pragma mark Fetch Request support
119 124
 
9  templates/machine.h.motemplate
@@ -73,6 +73,15 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch
73 73
 <$foreach FetchedProperty noninheritedFetchedProperties do$>
74 74
 @property (nonatomic, readonly) NSArray *<$FetchedProperty.name$>;
75 75
 <$endforeach do$>
  76
+<$if TemplateVar.frc$>
  77
+#if TARGET_OS_IPHONE
  78
+<$foreach Relationship noninheritedRelationships do$>
  79
+<$if Relationship.isToMany$>
  80
+- (NSFetchedResultsController *)new<$Relationship.name.initialCapitalString$>FetchedResultsControllerWithSortDescriptors:(NSArray *)sortDescriptors;
  81
+<$endif$>
  82
+<$endforeach do$>
  83
+#endif
  84
+<$endif$>
76 85
 @end
77 86
 
78 87
 @interface _<$managedObjectClassName$> (CoreDataGeneratedAccessors)
23  templates/machine.m.motemplate
@@ -189,4 +189,27 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam
189 189
 }
190 190
 <$endif$>
191 191
 <$endforeach do$>
  192
+<$if TemplateVar.frc$>
  193
+#if TARGET_OS_IPHONE
  194
+<$foreach Relationship noninheritedRelationships do$>
  195
+<$if Relationship.isToMany$>
  196
+- (NSFetchedResultsController *)new<$Relationship.name.initialCapitalString$>FetchedResultsControllerWithSortDescriptors:(NSArray *)sortDescriptors {
  197
+	NSFetchRequest *fetchRequest = [NSFetchRequest new];
  198
+	<$if !TemplateVar.arc$>[fetchRequest autorelease];<$endif$>
  199
+	fetchRequest.entity = [NSEntityDescription entityForName:@"<$Relationship.destinationEntity.name$>" inManagedObjectContext:self.managedObjectContext];
  200
+	fetchRequest.predicate = [NSPredicate predicateWithFormat:@"<$Relationship.inverseRelationship.name$> <$if Relationship.inverseRelationship.isToMany$>CONTAINS<$else$>==<$endif$> %@", self];
  201
+	fetchRequest.sortDescriptors = sortDescriptors;
  202
+	<$if indexedNoninheritedAttributes.@count > 0$>
  203
+	NSArray *indexedIDs = [NSArray arrayWithObjects:<$foreach Attribute indexedNoninheritedAttributes do$>self.<$Attribute.name$>, <$endforeach do$>nil];
  204
+	NSString *cacheName = [NSString stringWithFormat:@"mogenerator.<$managedObjectClassName$>.%@.<$Relationship.name$>.%@", indexedIDs, sortDescriptors];
  205
+	<$endif$>
  206
+	return [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
  207
+											   managedObjectContext:self.managedObjectContext
  208
+												 sectionNameKeyPath:nil
  209
+														  cacheName:<$if indexedNoninheritedAttributes.@count > 0$>cacheName<$else$>nil<$endif$>];
  210
+}
  211
+<$endif$>
  212
+<$endforeach do$>
  213
+#endif
  214
+<$endif$>
192 215
 @end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.