Realm Swift filter query based on list property

cloud

#1

Hi, I need some help with a query. Here is the business context:

There are two user types:

User Type 1: Show Manager

A show manager can create shows. Below you’ll find the necessary details of a Show object. As you can see every show has a property artistResponses which is a list of ArtistResponse.

class Show: Object {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var createdDate = Date()
    @objc dynamic var showManager: ShowManager!
    @objc dynamic var venueName = ""
    @objc dynamic var city = ""
    ...
    **let artistResponses = List<ArtistResponse>()**
 
    override static func primaryKey() -> String? {
        return "id"
    }

    ... 
}

User Type 2: Artists

An Artist object has a citiesOfInterest property and artists on the app should only be shown shows created by show managers where the show city is in the artist’s citiesOfInterest. The artist can then view these shows and respond, which creates an ArtistResponse object and adds it to the list seen above in the Show object. Below you’ll find the necessary details of an Artist as well as an ArtistResponse object.

class Artist: Object {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var createdDate = Date()
    **let citiesOfInterest = List<String>()**
}
class ArtistResponse: Object {
    @objc dynamic var id = UUID().uuidString
    @objc dynamic var createdDate = Date()
    @objc dynamic var show: Show!
    @objc dynamic var artist: Artist!
    **@objc dynamic var available = true**

    override static func primaryKey() -> String? {
        return "id"
    }
}

The query I’m having trouble with is related to determining if it’s a new show (i.e. the artist does not have an ArtistResponse associated to the show listing themselves as available or unavailable).

Here is an example:

A show manager named Bob creates a show where the city = “Austin”
An artist named Jim has citiesOfInterest = [“Austin”, “Seattle”, “San Francisco”]

Jim, the artist, comes onto the app and should see this new show listing posted by Bob, the show manager, because Jim’s citiesOfInterest contains “Austin” and the show’s city is equal to “Austin” and there is no artistResponse related to Jim.

I’ve researched and found that there are limitations with lists in Realm.


Please help with this query:

Realm.objects(Show.self).filter(“somehow determine that there is not an artistResponse in the artistResponses list that is related to Jim”)

Can someone please help me with this? I hope I have provided enough explanation.


#2

Thanks to romaonthego on the below github issue I was able to right the correct query. The trick was using NOT ANY

newShowListings = Constants.realm.objects(Show.self).filter(
            """
            NOT ANY artistResponses.artist.id == %@
            AND city IN %@ 
            AND showDate >= %@ 
            AND isCancelled == %@
            """, artist.id, artist.citiesOfInterest, today, false)