Skip to content

Commit

Permalink
Merge pull request #88 from danielctull/feature/FetchedResultsControl…
Browse files Browse the repository at this point in the history
…lerMethods

[NEW] Generate fetched results controllers for to-many relationships. (Daniel Tull)
  • Loading branch information
rentzsch committed Jan 25, 2012
2 parents daa4ed1 + b2ce79b commit c0a0ba4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mogenerator.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions templates/machine.h.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions templates/machine.m.motemplate
Original file line number Diff line number Diff line change
Expand Up @@ -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.