Dumbfounded
I was just shown an NSPredicate that dumbfounded me for several reasons:
- It’s using five nested
SUBQUERYexpressions. In other words, aSUBQUERYwithin aSUBQUERYwithin aSUBQUERYwithin aSUBQUERYwithin aSUBQUERY. - It works
- It’s reasonably fast
- Core Data correctly handles it on a SQLite store
- It’s the only way the author could accomplish what he needed in a “reasonable” way.
What sort of NSPredicate is this? Here you go:
static NSString *kCommonRequestFragment = @"SUBQUERY(things, $thing, SUBQUERY($thing.thingType.spams, $spam, SUBQUERY($spam.ham.eggs, $egg, SUBQUERY($egg.foo.bars, $bar, SUBQUERY($bars.baz.blips, $blip, $blip.bing.id == %@).@count > 0).@count > 0).@count > 0).@count > 0).@count > 0";
- (NSFetchRequest *)fetchRequestForSomeObjects {
NSFetchRequest *theRequest = [self templateFetchRequest];
NSString * thePredicateFormatString = [NSString stringWithFormat:@"(%@) AND (%@)", @"SUBQUERY(participatingObjects, $object, $object.property.id == %@).@count > 0", kCommonRequestFragment];
[theRequest setPredicate:[NSPredicate predicateWithFormat:thePredicateFormatString, self.aThing.anID, self.aThing.anotherID]];
return theRequest;
}
- (NSFetchRequest *)fetchRequestForOtherObjects {
NSFetchRequest *theRequest = [self templateFetchRequest];
NSString * thePredicateFormatString = [NSString stringWithFormat:@"(%@) AND (%@)", @"SUBQUERY(participatingObjects, $object, $object.property.id == %@).@count == 0", kCommonRequestFragment];
[theRequest setPredicate:[NSPredicate predicateWithFormat:thePredicateFormatString, self.aThing.anID, self.aThing.anotherID]];
return theRequest;
}
Insane.
-
mabeldelauder likes this
-
jetrois likes this
-
phoenix-hurricane likes this
-
mike3k likes this
-
quatermain likes this
-
matsuda likes this
-
kzhu likes this
-
benzado likes this
-
andthirdly likes this
-
alexeckermann reblogged this from funwithobjc
-
funwithobjc posted this
Short URL for this post: http://tmblr.co/Zt522y6oX7Js