Alex Pretzlav
Alex Pretzlav

Reputation: 15615

Realm Predicate for Membership in List

I am trying to use RealmGridController to display elements in a relationship list. I have an object Collection that has a list of Items:

class Collection: Object {
    let items = List<Item>()

    dynamic var name: String?
}

class Item: Object {
    dynamic var name: String?
}

I would like to use a RealmGridController to show all of the items in the items property of a specific collection. RealmGridController uses RBQFetchedResultsController to manage its data loading, and the fetched results controller uses an entity name and a predicate to fetch the list of items to show.

How can I express the list of items in a relationship List as an entity name and predicate?

A few things I've tried that haven't worked:

entityName = Item.className()

// 'Invalid predicate', reason: 'Predicate with IN operator must compare a KeyPath with an aggregate'
basePredicate = NSPredicate(format: "SELF in %@.items", collection)
// Same
basePredicate = NSPredicate(format: "SELF in %@", collection.items)
// 'Invalid predicate', reason: 'Predicate with ANY modifier must compare a KeyPath with RLMArray with a value'
basePredicate = NSPredicate(format: "ANY SELF in %@", collection.items)

It seems like displaying a Realm List<> as the contents of a fetched results controller should be fairly straightforward but I just can't seem to figure it out. Thanks!

Upvotes: 0

Views: 650

Answers (1)

Adam Fish
Adam Fish

Reputation: 1543

RBQFetchedResultsController doesn't support using a List as the data source. I spent some time working on this to explore using KVO to manage the notifications vs. RBQRealmNotificationManager but never finished it.

For now, I would suggest looking into Realm's new collection notification support added in v0.98.0. You can register to receive a notification when any object in the List is updated. This still doesn't offer the index change information, but this is coming soon.

Upvotes: 1

Related Questions