Sort by nested ID of list only


#1

Hi there,

I have done some independent research across the various sites but I’m struggling to come up with a solution that allows me to use an OrderedRealmCollection for the use in a RealmRecyclerViewAdapter.

My basic data structure is as follows:

MediaItem
int ID
RealmList<CategoryTag> CategoryTags

CategoryTag
int CategoryID
int MediaItemID
int SortOrder

Category
int ID
String Name
int SortOrder

I have a list of Categories where a MediaItem can appear in many different categories via the Tags.

However, I wish to sort inside of those categories for an order of display for the MediaItem.

In SQL I would join CategoryTag on the CategoryID and MediaItemID I’m using and sort by the only that specific object within any given MediaItem list, is there a similar solution available in Realm or am I approaching the issue in the wrong way?

Thanks for any help or assistance on this matter.


#2

In realm, you can define (parent -> child) relations as Lists in the parent object.
Also, you can do the reverse of it, the (child -> parent) relation,
with inverse relationships, called LinkingObjects.

You can find more info here: https://realm.io/docs/swift/latest/#inverse-relationships