How to apply multiple filters to realm database results

杀马特。学长 韩版系。学妹 提交于 2020-07-10 07:47:04

问题


I am new to programming & trying to apply filters using UISwitches

Currently I have:

  1. A realm database returning all the objects
  2. A UI that uses Switches to send filters

Things Attempted:

  1. Subqueries, however I don't know the proper format to write it for realm

Goal: Apply multiple filters to realm database results

User Interface:

Realm Database:

class Question: Object {

@objc dynamic var neverAttempted: Bool = true
@objc dynamic var correct: Int = 0
@objc dynamic var flagged: Bool = false
@objc dynamic var subject: String = ""

}

correct = 1;
neverAttempted = true;
flagged = false;
subject = Law;

View Controller:

import UIKit
import RealmSwift

class OptionsViewController: UIViewController {

@IBOutlet weak var totalQuestionsLabel: UILabel!

@IBOutlet weak var totalQuestionSlider: UISlider!

var neverAttempted = ""
var correct = ""
var flagged = false
var subject = ""

let realmOne = try! Realm()
var questionBank: Results<Question>! {
    get {
        return realmOne.objects(Question.self)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

}

func updateQuestionBank() {

    let updatedQuestionBank = realmOne.objects(Question.self)
        .filter(//Apply filter//)

    totalQuestionsLabel.text = String(updatedQuestionBank.count)
    print (updatedQuestionBank)

}

@IBAction func subjectSwitch(_ sender: UISwitch) {

    if sender.isOn == true {
        subject = 
        updateQuestionBank()
    }

}

@IBAction func flaggedSwitch(_ sender: UISwitch) {

    if sender.isOn == true {
        flagged =
        updateQuestionBank()
    }

}

@IBAction func neverAttemptedSwitch(_ sender: UISwitch) {

    if sender.isOn == true {
        neverAttempted = 

    }

}


@IBAction func correctOnlyOnceSwitch(_ sender: UISwitch) {

    if sender.isOn == true {
        correct = 
        updateQuestionBank()
    }

}

来源:https://stackoverflow.com/questions/62247520/how-to-apply-multiple-filters-to-realm-database-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!