I have no idea how to combine predicate based on user selection. Here is an overview of my data.
Person.swift
class Person: Object {
let id = RealmOptional<Int>()
dynamic var name = ""
dynamic var console = ""
override static func primaryKey() -> String {
return "id"
}
sample json data
{
"id": 822,
"name": "Ron",
"console": "XBox"
},
{
"id": 823,
"name": "Jenny"
"console": "Playstation 4"
}
say for example i have alot of data and console consist of either "XBox", "Playstation 4", "Wii", "Sega" or "PS Vita". I create a checkbox filtering option to allow user to selected the option they want to filter and show the name of the person who owns which console.
For example if they select both "XBox" and "Playstation 4" how do i predicate them both and show the result of the person name? Currently i can only show one option like this.
var player: Results<Person>!
func filter(sender: AnyObject) {
let realm = try! Realm()
self.player = realm.objects(Person).filter("%K = %@", "console", "XBox")
}
Predicates in Swift 1 Filtering a collection of models. As an example, let’s say that we’re building a classic todo app that lets our users organize their tasks and todo items using multiple lists ... 2 A case for predicates. ... 3 Expressive operators. ... 4 Composition built-in. ... 5 An expandable pattern. ... 6 Conclusion. ...
Many applications use Realm Swift – from chart-topping apps in the App Store to the world’s smallest cloud-hosted, SwiftUI chat app. Learn how Realm helps teams build better apps, faster. “ Realm makes the data persistence layer so easy on iOS. We have a big advantage in terms of developer speed and general feature development.
However, the kind of predicates that we explored in this article are really only suitable for querying in-memory data, as it’d be very hard to encode free-form closures into something that could be passed to a server, or to an on-disk database.
This way objects will be filtered before doing more heavy look ups. When using a predicate on an array, SELF refers to each object in the array. Here’s an example: Imagine you are a landlord figuring out which apartments have to pay their water bill.
You shouldn't dynamically craft an NSPredicate query string. It's much easier (and safer) to create an NSCompoundPredicate
programmatically. You can pass NSPredicate
s to Realm's RealmCollectionType.filter(...)
method. Here's an example to get all Person
s who have either an Xbox or a Playstation 4:
// could use .AndPredicateType here too, depending on what you want
let query = NSCompoundPredicate(type: .OrPredicateType, subpredicates: [NSPredicate(format: "console = 'Xbox'"), NSPredicate(format: "console = 'Playstation 4')])
let player = realm.objects(Person).filter(query)
Although in your case, you might be better off using an IN
query if you want OR
semantics:
let selectedConsoles = ["Xbox", "Playstation 4"]
let player = realm.objects(Person).filter("console IN %@", selectedConsoles)
Swift 5
Multiple Predicate
combine predicates together with NSCompoundPredicate:
let predict = NSPredicate.init(format: "console == %@", "Xbox")
let predict2 = NSPredicate.init(format: "console == %@", "Playstation 4")
let query = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.or, subpredicates: [predict,predict2])
let player = realm.objects(Person).filter(query)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With