Skip to content

Instantly share code, notes, and snippets.

@MarcoSero
Forked from ryancole/foo.m
Created April 20, 2013 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcoSero/5425907 to your computer and use it in GitHub Desktop.
Save MarcoSero/5425907 to your computer and use it in GitHub Desktop.
Convert NSPredicate into NSDictionary for use as a query string in an AFNetworking HTTP request
- (NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest
withContext:(NSManagedObjectContext *)context {
// init the query string dictionary
NSMutableDictionary *queryString = nil;
// if we're given a predicate, convert it to a dictionary
if (fetchRequest.predicate) {
if ([fetchRequest.predicate isKindOfClass:[NSCompoundPredicate class]]) {
// init the query string
queryString = [[NSMutableDictionary alloc] init];
// get the predicate
NSCompoundPredicate *predicate = (NSCompoundPredicate *)fetchRequest.predicate;
// set the query string values
for (NSComparisonPredicate *comparison in predicate.subpredicates) {
[queryString setValue:comparison.rightExpression.constantValue forKey:comparison.leftExpression.keyPath];
}
} else if ([fetchRequest.predicate isKindOfClass:[NSComparisonPredicate class]]) {
// init the query string
queryString = [[NSMutableDictionary alloc] init];
// get the predicate
NSComparisonPredicate *predicate = (NSComparisonPredicate *)fetchRequest.predicate;
// set the query string values
[queryString setValue:predicate.rightExpression.constantValue forKey:predicate.leftExpression.keyPath];
}
}
return [self requestWithMethod:@"GET" path:[self pathForEntity:fetchRequest.entity] parameters:queryString];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment