Fawkes
Fawkes

Reputation: 3941

Realm subquery with List count in Swift

I have

class Car : Object
{
     let queries : List<QueryModel> = List<QueryModel>()
}

and

class QueryModel : Object
{
    let location : String = ""
}

Now how do I write that NSPredicate to fetch just that cars which have queries with location equal to 'London'?

p.s. It's a known fact that things like @count do not work on Swift List class...

p.p.s. I need it as a predicate because this is the only way RBQFetchedResultsController knows how to do it's job.

How I thought it could've been written was something like NSPredicate(format: "SUBQUERY(queries, $query, $query.location == %@)", 'London'). But nothing like this ever passes Realm's predicate validation :)

Upvotes: 1

Views: 1677

Answers (1)

Mikael
Mikael

Reputation: 2395

You can write

NSPredicate(format: "ANY queries.location = %@", locationString)

If you want to add other filtering, you can create other NSPredicate and then use

NSCompoundPredicate(andPredicateWithSubpredicates: [pred1, pred2])

to combine the filters.

Now, if you want to filter cars, you have to prepare a

let request = FetchRequest<Car>(realm: realm, predicate: predicate)

then you can use RBQFetchedResultsController to execute the request.

Upvotes: 3

Related Questions