diff --git a/mogenerator.m b/mogenerator.m index 029e42b0..63b0fc5c 100644 --- a/mogenerator.m +++ b/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 diff --git a/templates/machine.h.motemplate b/templates/machine.h.motemplate index b996b03c..29f40dc6 100644 --- a/templates/machine.h.motemplate +++ b/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) diff --git a/templates/machine.m.motemplate b/templates/machine.m.motemplate index 71f9f3fa..6e28dc60 100644 --- a/templates/machine.m.motemplate +++ b/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