Reputation: 20538
I am building an iOS app using Swift. I am using Realm as database.
I am currently building a search functionality for a tableview.
This is my filter query
items = realm.objects(Book).filter(predicate).filter("state IN {'pending','activated','completed','closed'}")
I am saving what states a user wants to filter on in another model called Filter.
How can I build this {'pending','activated','completed','closed'} from the output of the following filter query (title is the attribute)? What is this object called?
realm.objects(Filter).filter("type = 'filter' AND activated = 'true'")
Upvotes: 2
Views: 1915
Reputation: 18308
The right-hand side of the IN
operator can take a substitution placeholder (%@
) that can have an NSArray
(or other enumerable object) substituted in.
Assuming your Filter
model looks something like:
class Filter: Object {
dynamic var type: String = ""
dynamic var activated: bool = false
dynamic var state: String = ""
}
You can do something like the following to construct the query you're after:
let activeFilters = realm.objects(Filter).filter("type = 'filter' AND activated = 'true'")
let activeStates = activeFilters.valueForKey("state") as! [String]!
let items = realm.objects(Book).filter(predicate).filter("state IN %@", activeStates)
Upvotes: 3