Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Generate fetched results controllers for to-many relationships #88

Merged
merged 6 commits into from

2 participants

@danielctull

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.

@danielctull danielctull 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
@danielctull

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.

@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.

@danielctull

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

@danielctull

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

@rentzsch rentzsch merged commit c0a0ba4 into rentzsch:master
@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
Commits on Jan 24, 2012
  1. @danielctull

    Generate fetched results controllers for to-many relationships

    danielctull authored
    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.
Commits on Jan 25, 2012
  1. @danielctull

    Fix to test for not arc

    danielctull authored
  2. @danielctull
  3. @danielctull
  4. @danielctull

    Revert "Add key value coding for count on NSArray"

    danielctull authored
    This reverts commit 9fa14f961771ef408ed0ea29f7ab1b7c059f8830.
  5. @danielctull
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.