Created
September 4, 2012 12:00
-
-
Save ikura/3620606 to your computer and use it in GitHub Desktop.
Parse workaround for "isPrefixOf" query
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
@implementation ViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
[Parse setApplicationId:kDevParseAppId clientKey:kDevParseClientKey]; | |
#ifdef CREATE_TESTDATA | |
NSArray * data = @[ | |
@"12345", //A | |
@"12346", //B | |
@"1234", //C | |
@"2345", //D | |
@"23451" //E | |
]; | |
NSMutableArray *testObjects = [@[] mutableCopy]; | |
for (NSString *testObject in data) { | |
PFObject *object = [PFObject objectWithClassName:@"TestClass"]; | |
[object setObject:testObject forKey:@"TestKey"]; | |
[testObjects addObject:object]; | |
} | |
[PFObject saveAll:testObjects]; | |
#endif | |
NSArray * search = @[ // Expected Results | |
@"12345", // [A,C] | |
@"123456", //[A,C] | |
@"234", // [] | |
@"2345222", // [D] | |
@"2345111", // [D, E] | |
]; | |
for (NSString *searchString in search) | |
{ | |
PFQuery *prefixOf = [self key:@"TestKey" isPrefixOf:searchString inClassName:@"TestClass"]; | |
PFQuery *prefixOfUsingRegex = [self key:@"TestKey" isPrefixOfUsingRegex:searchString inClassName:@"TestClass"]; | |
PFQuery *hasPrefix = [PFQuery queryWithClassName:@"TestClass"]; | |
[hasPrefix whereKey:@"TestKey" hasPrefix:searchString]; | |
NSLog(@"PrefixOf %@ results: %@", searchString, prefixOf.findObjects); | |
NSLog(@"PrefixOfUsingRegex %@ results: %@", searchString, prefixOfUsingRegex.findObjects); | |
NSLog(@"HasPrefix %@ results: %@", searchString, hasPrefix.findObjects); | |
} | |
} | |
// hasPrefix - returns objects where values of key being with searchString: key = <searchString>* | |
// Return objects where values of key are prefixes of searchString: key* = searchString | |
- (PFQuery *)key:(NSString *)key isPrefixOf:(NSString *)searchString inClassName:(NSString *)className | |
{ | |
NSMutableArray *queries = [@[] mutableCopy]; | |
for (int i=1; i<=searchString.length; i++) | |
{ | |
NSString *match = [searchString substringWithRange:NSMakeRange(0, i)]; | |
PFQuery *query = [PFQuery queryWithClassName:className]; | |
[query whereKey:key equalTo:match]; | |
[queries addObject:query]; | |
} | |
return [PFQuery orQueryWithSubqueries:queries]; | |
} | |
// Return objects where values of key are prefixes of searchString: key* = searchString | |
- (PFQuery *)key:(NSString *)key isPrefixOfUsingRegex:(NSString *)searchString inClassName:(NSString *)className | |
{ | |
NSString *regex = nil; | |
for (int i=1; i<=searchString.length; i++) | |
{ | |
NSString *match = [searchString substringWithRange:NSMakeRange(0, i)]; | |
if (regex == nil) regex = match; | |
else regex = [NSString stringWithFormat:@"%@|%@", regex, match]; | |
} | |
regex = [NSString stringWithFormat:@"^(%@)$", regex]; | |
PFQuery *query = [PFQuery queryWithClassName:className]; | |
[query whereKey:key matchesRegex:regex]; | |
NSLog(@"Regex to match prefix of %@ is %@", searchString, regex); | |
return query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2012-09-04 20:03:35.714 parseScaffold[22384:c07] Regex to match prefix of 12345 is ^(1|12|123|1234|12345)$
2012-09-04 20:03:40.515 parseScaffold[22384:c07] PrefixOf 12345 results: (
"TestClass:WFjd8Uz883 {\n TestKey = 1234;\n}",
"TestClass:jKUQyyPFI2 {\n TestKey = 12345;\n}"
)
2012-09-04 20:03:41.435 parseScaffold[22384:c07] PrefixOfUsingRegex 12345 results: (
"TestClass:WFjd8Uz883 {\n TestKey = 1234;\n}",
"TestClass:jKUQyyPFI2 {\n TestKey = 12345;\n}"
)
2012-09-04 20:03:42.664 parseScaffold[22384:c07] HasPrefix 12345 results: (
"TestClass:jKUQyyPFI2 {\n TestKey = 12345;\n}"
)
2012-09-04 20:03:42.664 parseScaffold[22384:c07] Regex to match prefix of 123456 is ^(1|12|123|1234|12345|123456)$
2012-09-04 20:03:43.893 parseScaffold[22384:c07] PrefixOf 123456 results: (
"TestClass:WFjd8Uz883 {\n TestKey = 1234;\n}",
"TestClass:jKUQyyPFI2 {\n TestKey = 12345;\n}"
)
2012-09-04 20:03:44.814 parseScaffold[22384:c07] PrefixOfUsingRegex 123456 results: (
"TestClass:WFjd8Uz883 {\n TestKey = 1234;\n}",
"TestClass:jKUQyyPFI2 {\n TestKey = 12345;\n}"
)
2012-09-04 20:03:45.736 parseScaffold[22384:c07] HasPrefix 123456 results: (
)
2012-09-04 20:03:45.736 parseScaffold[22384:c07] Regex to match prefix of 234 is ^(2|23|234)$
2012-09-04 20:03:46.657 parseScaffold[22384:c07] PrefixOf 234 results: (
)
2012-09-04 20:03:47.579 parseScaffold[22384:c07] PrefixOfUsingRegex 234 results: (
)
2012-09-04 20:03:48.501 parseScaffold[22384:c07] HasPrefix 234 results: (
"TestClass:aTdP84wPxb {\n TestKey = 2345;\n}",
"TestClass:UGVlIAyj6e {\n TestKey = 23451;\n}"
)
2012-09-04 20:03:48.501 parseScaffold[22384:c07] Regex to match prefix of 2345222 is ^(2|23|234|2345|23452|234522|2345222)$
2012-09-04 20:03:49.501 parseScaffold[22384:c07] PrefixOf 2345222 results: (
"TestClass:aTdP84wPxb {\n TestKey = 2345;\n}"
)
2012-09-04 20:03:50.652 parseScaffold[22384:c07] PrefixOfUsingRegex 2345222 results: (
"TestClass:aTdP84wPxb {\n TestKey = 2345;\n}"
)
2012-09-04 20:03:51.880 parseScaffold[22384:c07] HasPrefix 2345222 results: (
)
2012-09-04 20:03:51.880 parseScaffold[22384:c07] Regex to match prefix of 2345111 is ^(2|23|234|2345|23451|234511|2345111)$
2012-09-04 20:03:52.800 parseScaffold[22384:c07] PrefixOf 2345111 results: (
"TestClass:UGVlIAyj6e {\n TestKey = 23451;\n}",
"TestClass:aTdP84wPxb {\n TestKey = 2345;\n}"
)
2012-09-04 20:03:53.186 parseScaffold[22384:c07] PrefixOfUsingRegex 2345111 results: (
"TestClass:aTdP84wPxb {\n TestKey = 2345;\n}",
"TestClass:UGVlIAyj6e {\n TestKey = 23451;\n}"
)
2012-09-04 20:03:53.721 parseScaffold[22384:c07] HasPrefix 2345111 results: (
)