Khant Thu Linn
Khant Thu Linn

Reputation: 6143

iOS Doing subquery from RLMLinkingObjects

This is my attachment model.

@interface Attachment : RLMObject
@property BOOL isOfflineAvailable;
@property BOOL isStarred;

In my category, I set up linking to attachment.

@property (readonly) RLMLinkingObjects *relatedAttachments;

+ (NSDictionary *)linkingObjectsProperties {
    return @{
             @"relatedAttachments": [RLMPropertyDescriptor descriptorWithClass:Attachment.class propertyName:@"category"],
             };
}

Then, I am trying to query relatedAttachments and take only if isOfflineAvailable is true.

NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY relatedAttachments.isOfflineAvailable == YES"];

self.categories = [[Category objectsWithPredicate:pred] sortedResultsUsingProperty:@"id" ascending:YES];

Problem is that it is empty (although there is data). How shall I do? If it is not linking object and just RLMArray, it is okay.

Upvotes: 0

Views: 99

Answers (1)

TiM
TiM

Reputation: 16021

RLMLinkingObjects represents a collection, just like an array, instead of a single object. As such, trying to call a predicate directly on relatedAttachments won't work, because as an array, it won't have the isOfflineAvailable property.

Instead, you can use the SUBQUERY syntax of NSPredicate to query for any objects inside relatedAttachments who match that criteria:

RLMResults *categories = [Category objectsWhere:@"SUBQUERY(relatedAttachments, $attachment, $attachment.isOfflineAvailable == YES).@count > 0"];

Upvotes: 1

Related Questions