Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4: Fetch Request Template Variables?

In Xcode 3.X, you were supposed to right-click the whitespace in the fetch request template's predicate editor to specify a variable input rather than a hard-coded predicate.

Where is this in XCode 4? I've held option, right-clicked, option-clicked, etc and cannot figure it out....

Variable in Fetch Request Template Editor Predicate

like image 525
Jesse Bunch Avatar asked Aug 17 '11 21:08

Jesse Bunch


2 Answers

I don't think X4 has the variable anymore.

Instead, I think you have to choose an expression and then provide a variable of the form $VARNAME.

For example, given and entity Alpha with an attribute aString, I created a fetch request template bobFetch with an expression of aString == $TESTVAR.

Alpha *a=[NSEntityDescription insertNewObjectForEntityForName:@"Alpha" inManagedObjectContext:self.moc];
a.aString=@"steve";
[self saveContext];
NSDictionary *subVars=[NSDictionary dictionaryWithObject:@"steve" forKey:@"TESTVAR"];
NSFetchRequest *fetchRequest =   [self.managedObjectModel fetchRequestFromTemplateWithName:@"bobRequest" substitutionVariables:subVars];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alpha" inManagedObjectContext:self.moc];
[fetchRequest setEntity:entity];

If logged fetchRequest reports:

<NSFetchRequest: 0x4d17480> (entity: Alpha; predicate: (aString == "steve"); sortDescriptors: ((null)); type: NSManagedObjectResultType; )

... and can then be used normally.

NSError *error = nil;
NSArray *fetchedObjects = [self.moc executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
    NSLog(@"fetch error = %@",error);
}
NSLog(@"fetchObjects = %@",fetchedObjects);

Kind of clumsy for a graphical environment but it works.

like image 135
TechZen Avatar answered Oct 14 '22 20:10

TechZen


This has been tweaked in Xcode 4. In order to use substitution variables, you need to choose "Expression" from the popup menu (i.e. instead of an attribute name) and you can enter the equivalent like this: name == $SEARCH_NAME

If you were to just enter a $VARIABLE value in the field for each attribute, you'll get the wrong result. In fact, some attributes won't allow that such as Date attributes where you are forced to enter a value.

Of course you can use multiple variables from there on.

Then it's just as before with executing the fetch request:

NSString *searchName = @"Mr Squiggle";
NSDictionary *subs = [NSDictionary dictionaryWithObject:searchName forKey:@"SEARCH_NAME"];
NSManagedObjectModel *model = [self managedObjectModel];
NSFetchRequest *req = [model fetchRequestFromTemplateWithName:@"trainerByName" substitutionVariables:subs];
NSError *error = nil;
NSArray *results = [[self managedObjectContext] executeFetchRequest:req error:&error];
NSLog(@"Found %ld record.", [results count]);

Note you can also do away with the attributes popup and just click the button on the top right of the editor (looks like lines right beside the default grid view button) and just enter your expression straight away. This is a good way of seeing how some things like dates get translated.

like image 39
smaurice Avatar answered Oct 14 '22 22:10

smaurice