-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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