Skip to content

Commit

Permalink
Generate fetched results controllers for to-many relationships
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danielctull committed Jan 24, 2012
1 parent 2acbf9d commit 99ec596
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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.frc$>[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;
NSArray *indexedIDs = [NSArray arrayWithObjects:<$foreach Attribute noninheritedAttributes do$><$if Attribute.isIndexed$>self.<$Attribute.name$>, <$endif$><$endforeach do$>nil];
NSString *cacheName = nil;
if ([indexedIDs count] > 0)
cacheName = [NSString stringWithFormat:@"mogenerator.<$managedObjectClassName$>.%@.<$Relationship.name$>.%@", indexedIDs, sortDescriptors];
return [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:cacheName];
}
<$endif$>
<$endforeach do$>
#endif
<$endif$>
@end

0 comments on commit 99ec596

Please sign in to comment.