COOKIES
COOKIES

Reputation: 444

Query nested object array for property with Realm

I've been trying to write a query to access an object equal to a specific key for a while without success. I've already looked through SO & Realm documentation without any luck.

If I print out the structure of the CachedCodes RLMObject, it looks like this:

CachedCodes {
    Codes = Codes {
            assets = RLMArray<KVString> (
                [0] KVString {
                    key = Some Key;// this is what I'd want to query for
                    value = Some Value;
                }
            );
        };
}

Here's my class structure:

@interface CachedCodes : RLMObject
@property (nonatomic, strong, readonly) Codes *Codes;//readwrite in .m
@end

@interface Codes : RLMObject
@property (nonatomic, strong, readonly) RLMArray<KVString *><KVString> *assets;//readwrite in .m
@end

@interface KVString : RLMObject
@property (nonatomic, strong, readonly) NSString *key;//readwrite in .m
@property (nonatomic, strong, readonly) NSString *value;//readwrite in .m
@end

RLM_ARRAY_TYPE(KVString);

Here's what I've tried:

[CachedObjects objectsWhere:@"Codes.assets.key == 'Some key'"];

Error: Key paths that include an array property must use aggregate operations.

[CachedObjects objectsWhere:@"Codes.assets.key IN 'Some key'"];

Error: Key paths that include an array property must use aggregate operations.

[CachedObjects objectsWhere:@"ANY Codes.assets.key == 'Some key'"];

Error: No error. It doesn't crash, nor does it give me the objects with key "Some key". Instead, it gives me all of the objects.

If anyone has any input, I'd be forever grateful :)

Upvotes: 0

Views: 414

Answers (1)

christopherdrum
christopherdrum

Reputation: 1523

After some back-and-forth discussion, modified solution is presented here. The long and the short of it is that Realm provides a way to grab inner-nested objects of a specific type within a realm directly without having to worry so much about the exact key-path to get there.

RLMResults<KVString *> *results = [KVString objectsWhere:@"key == 'SomeKey'"];

Upvotes: 1

Related Questions