Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RealmSwift + Multiple Predicate

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")
}
like image 840
Delonn Avatar asked Dec 07 '15 11:12

Delonn


People also ask

How to use predicates in Swift?

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. ...

Why use realm Swift?

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.

Are predicates suitable for querying in-memory data?

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.

What is a self predicate in JavaScript?

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.


2 Answers

You shouldn't dynamically craft an NSPredicate query string. It's much easier (and safer) to create an NSCompoundPredicate programmatically. You can pass NSPredicates to Realm's RealmCollectionType.filter(...) method. Here's an example to get all Persons 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)
like image 99
jpsim Avatar answered Sep 27 '22 00:09

jpsim


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)
like image 45
Rushang Patel Avatar answered Sep 26 '22 00:09

Rushang Patel