My models look like this:
@interface FeedItem : RLMObject
@property NSString *title;
@property NSString *uniqueIdentifier;
@property BOOL isUnread;
@property Feed *feed;
@end
RLM_ARRAY_TYPE(FeedItem)
RLM_ARRAY_TYPE(Folder)
@interface Feed : RLMObject
@property NSString *title;
@property NSString *uniqueIdentifier;
@property RLMArray<FeedItem *><FeedItem> *items;
@property RLMArray<Folder *><Folder> *folders;
@end
RLM_ARRAY_TYPE(Feed)
@interface Folder : RLMObject
@property NSString *title;
@property NSString *uniqueIdentifier;
@property RLMArray<Feed *><Feed> *feeds;
@end
All relationships are set explicitly (i.e. I do not use backlinks), and before adding a Folder to a Feed's 'folders' property, or, before adding a Feed to a Folder's 'feeds' property, I check if one already exists to avoid duplication.
Now, I have a Folder, and need to fetch all FeedItem objects that are related to all Feed objects related to this Folder in this folder. I have tried every kind of predicate format I could think of..
From a simple:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ IN feed.folders", folder];
RLMResults *feedItems = [FeedItem objectsWithPredicate:predicate];
which fails with exception 'Invalid predicate', reason: 'Predicate with IN operator must compare a KeyPath with an aggregate'.
to a SUBQUERY like:
NSString *folderUniqueIdentifier = folder.uniqueIdentifier;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(feed.folders, $folder, $folder.uniqueIdentifier = %@).@count > 0", folderUniqueIdentifier];
RLMResults *feedItems = [FeedItem objectsWithPredicate:predicate];which fails with exception 'Invalid predicate', reason: 'Aggregate operations can only be used on RLMArray properties'.
The only one I have been able to get to work is:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"feed IN %@", folder.feeds];
RLMResults *feedItems = [FeedItem objectsWithPredicate:predicate];But, this one also does not seem to auto-update when feeds are added to, or removed from the Folder's 'feeds' property. (I end up having to add a KVO observer to the Folder's 'feeds' property and execute the query again when said observer notifies of a change).
So, any help with constructing a query for this that can auto-update would be greatly appreciated.