#####Adds support for SUBQUERY in Realm queries.
Need to query a RLMObject
against a to-many relationship using multiple properties? This category brings the hidden gem in NSPredicate
, SUBQUERY
, to Realm. For example, given this Realm setup:
Person : RLMObject
@property NSString *Id;
@property RLMArray<Dog> *dogs;
Dog : RLMObject
@property BOOL hasFleas;
@property NSString *color;
it is now possible to write a query for all the Person(s) that have at least one Dog with fleas and is colored brown. This makes use of SUBQUERY
in the resulting Realm query:
RLMResults *ownersOfBrownDogsWithFleas = [Person objectsWithSubqueryPredicate:@"SUBQUERY(dogs, $dog, $dog.hasFleas = YES AND $dog.color == 'brown').@count > 0"];
####Format
The format is similar to the standard NSPredicate SUBQUERY
expression:
SUBQUERY(RLMArray keypath, variable, predicate)
####Installation RBQFetchedResultsController is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RealmSubqueryHelper'
####Demo
Build and run/test the Example project in Xcode to see a working SUBQUERY
. This project uses CocoaPods. If you don't have CocoaPods installed, grab it with [sudo] gem install cocoapods.
git clone https://github.com/bigfish24/RealmSubqueryHelper
cd RealmSubqueryHelper/objc-example
pod install
open RBQFetchedResultsControllerExample.xcworkspace
#####Requirements
- iOS 7+
####Limitations
- The base object must have a primary key (above,
Person
has primary keyId
). - Only the
@count
collection operator is supported. - Compound predicates with
SUBQUERY
are supported, but nestedSUBQUERY
is not.