Get unique related object

38 views
Skip to first unread message

Craig Siemens

unread,
Oct 5, 2015, 12:06:39 PM10/5/15
to Realm
My data model is setup like this:
- Conversation has many Messages
- Message has a User

What I want to do is get all the Users that have replied to a Conversation. Normally I would use the collection operator @distinctUnionOfObjects but I looks like List doesn't support that.

My current solution looks like this.

let users = conversation.messages.valueForKeyPath("user") as! [User]
let uniqueUsers
= NSOrderedSet(array: bodies)

Is this the best way to be doing this? My only concern is that the users array will have a size of messages.count which seems unnecessary if I just want to get the unique users.

JP Simard

unread,
Oct 5, 2015, 4:45:41 PM10/5/15
to Craig Siemens, realm...@googlegroups.com
Hi Craig, I assume you meant to write `NSOrderedSet(array: users)`? You're right that Realm queries don't support `@distinctUnionOfObjects` at the moment (we're tracking this as #1103).

As long as you don't access the users array after you initialize the NSOrderedSet, it'll automatically be released by ARC, so you shouldn't need to worry about that sticking around for too long.

--
JP Simard



--
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/c00656da-3709-40e9-a3ca-2ea2276f4ae4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:127868343-1982#}
Reply all
Reply to author
Forward
0 new messages