Permalink
Browse files

Merge pull request #88 from danielctull/feature/FetchedResultsControl…

…lerMethods

[NEW] Generate fetched results controllers for to-many relationships. (Daniel Tull)
  • Loading branch information...
2 parents daa4ed1 + b2ce79b commit c0a0ba4a75f7bc74de7256e0c39ec74a497a1f0e @rentzsch committed Jan 25, 2012
Showing with 37 additions and 0 deletions.
  1. +5 −0 mogenerator.m
  2. +9 −0 templates/machine.h.motemplate
  3. +23 −0 templates/machine.m.motemplate
View
5 mogenerator.m
@@ -114,6 +114,11 @@ - (NSArray*)noninheritedFetchedProperties {
return [[[self fetchedPropertiesByName] allValues] sortedArrayUsingDescriptors:sortDescriptors];
}
}
+/** @TypeInfo NSAttributeDescription */
+- (NSArray*)indexedNoninheritedAttributes {
+ NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isIndexed == YES"];
+ return [[self noninheritedAttributes] filteredArrayUsingPredicate:predicate];
+}
#pragma mark Fetch Request support
View
9 templates/machine.h.motemplate
@@ -73,6 +73,15 @@ extern const struct <$managedObjectClassName$>FetchedProperties {<$foreach Fetch
<$foreach FetchedProperty noninheritedFetchedProperties do$>
@property (nonatomic, readonly) NSArray *<$FetchedProperty.name$>;
<$endforeach do$>
+<$if TemplateVar.frc$>
+#if TARGET_OS_IPHONE
+<$foreach Relationship noninheritedRelationships do$>
+<$if Relationship.isToMany$>
+- (NSFetchedResultsController *)new<$Relationship.name.initialCapitalString$>FetchedResultsControllerWithSortDescriptors:(NSArray *)sortDescriptors;
+<$endif$>
+<$endforeach do$>
+#endif
+<$endif$>
@end
@interface _<$managedObjectClassName$> (CoreDataGeneratedAccessors)
View
23 templates/machine.m.motemplate
@@ -189,4 +189,27 @@ const struct <$managedObjectClassName$>FetchedProperties <$managedObjectClassNam
}
<$endif$>
<$endforeach do$>
+<$if TemplateVar.frc$>
+#if TARGET_OS_IPHONE
+<$foreach Relationship noninheritedRelationships do$>
+<$if Relationship.isToMany$>
+- (NSFetchedResultsController *)new<$Relationship.name.initialCapitalString$>FetchedResultsControllerWithSortDescriptors:(NSArray *)sortDescriptors {
+ NSFetchRequest *fetchRequest = [NSFetchRequest new];
+ <$if !TemplateVar.arc$>[fetchRequest autorelease];<$endif$>
+ fetchRequest.entity = [NSEntityDescription entityForName:@"<$Relationship.destinationEntity.name$>" inManagedObjectContext:self.managedObjectContext];
+ fetchRequest.predicate = [NSPredicate predicateWithFormat:@"<$Relationship.inverseRelationship.name$> <$if Relationship.inverseRelationship.isToMany$>CONTAINS<$else$>==<$endif$> %@", self];
+ fetchRequest.sortDescriptors = sortDescriptors;
+ <$if indexedNoninheritedAttributes.@count > 0$>
+ NSArray *indexedIDs = [NSArray arrayWithObjects:<$foreach Attribute indexedNoninheritedAttributes do$>self.<$Attribute.name$>, <$endforeach do$>nil];
+ NSString *cacheName = [NSString stringWithFormat:@"mogenerator.<$managedObjectClassName$>.%@.<$Relationship.name$>.%@", indexedIDs, sortDescriptors];
+ <$endif$>
+ return [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
+ managedObjectContext:self.managedObjectContext
+ sectionNameKeyPath:nil
+ cacheName:<$if indexedNoninheritedAttributes.@count > 0$>cacheName<$else$>nil<$endif$>];
+}
+<$endif$>
+<$endforeach do$>
+#endif
+<$endif$>
@end

0 comments on commit c0a0ba4

Please sign in to comment.