简体   繁体   中英

Searching Arrays with Realm (iOS)

Hiho,

I have a question about querying a realm database. I have realm-objects which contains features in a RLMArray. I want to find all objects which contains all features from a given array. And in another case I want to find all object which have one of the features.

I try things like this (also with IN):

  NSArray *featureArray = @[@"feature1", @"feature2"];
  RLMResults* containedObjects = [MyObject objectsWhere:@"features CONTAINS %@", featureArray];

But get errors like: RLMArray predicates must use aggregate operations

I think the problem is, that realm have to check an array with an array for matching. In SQL I think a JOIN-Operation would be a good (not very efficent) way, but I can't found an equivalent operation in NSPredicate.

Thank you for every hint!

As you can't store arrays of values in Realm, I guess that MyObject would have in that case a to-many relation / list property to a class Feature , which might be identified by their name .

CONTAINS is with NSPredicate not what you're looking for here. This is a string comparison operator .

You can use the IN operator, as seen below:

NSArray *featureArray = @[@"feature1", @"feature2"];
RLMResults* containedObjects = [MyObject objectsWhere:@"features.name IN %@", featureArray];

Note: you don't have to worry about performance, as links are first-class citizen in Realm, there are no expensive JOIN operations involved here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM