Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Correct generation of fetch requests with repeated variables #125

merged 2 commits into from

2 participants


If a CoreData model has a fetch request with a predicate where some variables are repeated, mogenerator will now generate correct Objective-C code.

Example: Suppose a CoreData model has a fetch request with predicate (name==$NAME AND id==$ID ) OR ( surname==$NAME AND id="special"). In this predicate, the variable $NAME is mentioned twice. The current version of mogenerator exports such a fetch request as a function with declaration of this form,

  • (NSArray)fetchGetWhatever:(NSManagedObjectContext)moc_ NAME:(NSString*)NAME_ ID:(NSString *)ID_ NAME:(NSString *)NAME_;

This does not compile since the parameter "NAME_" is used twice.

My patch corrects this problem and makes sure that variables are used only once.


Hi Sergei,

Looks good. Could you remove the // changes by lines before I merge this? The diffs already do a good job of highlighting differences, your name is already on the commit and I promise to give you credit in the release notes :-)


Well then I'm glad I held your hand a little through the process. Welcome to forking :) Pulled.

@rentzsch rentzsch merged commit 4334e67 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 16, 2012
Commits on Aug 17, 2012
  1. removed superfluous comments.

    Sergei Winitzki authored
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 2 deletions.
  1. +14 −2 mogenerator.m
16 mogenerator.m
@@ -179,6 +179,16 @@ - (NSString *)_resolveKeyPathType:(NSString *)keyPath {
return [entity managedObjectClassName];
+// auxiliary function
+- (BOOL) bindingsArray:(NSArray *)bindings containsVariableNamed:(NSString *)name {
+ for (NSDictionary *dict in bindings) {
+ if ([[dict objectForKey:@"name"] isEqual:name]) {
+ return YES;
+ }
+ }
+ return NO;
- (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bindings_ {
if (!predicate_) return;
@@ -209,11 +219,13 @@ - (void)_processPredicate:(NSPredicate*)predicate_ bindings:(NSMutableArray*)bin
type = [self _resolveKeyPathType:[lhs keyPath]];
type = [type stringByAppendingString:@"*"];
- [bindings_ addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ // make sure that no repeated variables are entered here.
+ if (![self bindingsArray:bindings_ containsVariableNamed:[rhs variable]]) {
+ [bindings_ addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[rhs variable], @"name",
type, @"type",
+ }
} break;
assert(0 && "unknown NSExpression type");
Something went wrong with that request. Please try again.