Looking for help constructing a query.

58 views
Skip to first unread message

Kunal Sood

unread,
Feb 15, 2016, 2:40:37 AM2/15/16
to Realm
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.

Kunal Sood

unread,
Feb 17, 2016, 7:26:35 AM2/17/16
to Mark Rowe, realm...@googlegroups.com
Thanks Mark,

I notice that #3216 got merged into master, so gave it a shot, and it does exactly what what I was after.

Thanks again!

Best,

Kunal

On 16 Feb 2016, 3:56 AM +0530, Mark Rowe <he...@realm.io>, wrote:
Hi Kunal,

You mentioned three attempts at queries to retrieve FeedItem objects related to a given Folder:
  1. "%@ IN feed.folders". As you've seen, this form of expression is not currently supported by Realm. I've added it to the list on our Unsupported predicates GitHub issue.
  2. "SUBQUERY(feed.folders, …).@count > 0". We are currently erroneously rejecting this query. I've submitted a pull request to address this.
  3. "feed IN %@". As you've seen, this expression operates on a snapshot of the substituted RLMArray rather than the live contents as expected. I've filed a GitHub issue about this.
You'll need to stick with option 3 for now, but when #3216 ships you can switch to option 2 to avoid explicitly recreating the query in response to notifications.

--
Mark Rowe

{#HS:171302366-3042#}
--
You received this message because you are subscribed to the Google Groups "Realm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-cocoa...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-cocoa/3cc67fc6-8cfb-4a43-8d5c-5f05082af302%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages