Search code examples
iosswiftuitableviewrealmnspredicate

UITableView doesn't show the realm objects that it should show


I have a problem with the content of a tableView. It doesn't show me the realm objects, that I asked for. Here's my code: At the top of the class i declared this variable:

let newPlan = TrainingPlan()

Then I have this button action, which is copying the exercise objects, put them in an array an add this to realm. I did this, because I only want to change this copied array:

 @IBAction func savePlanAction(_ sender: AnyObject) {
    if planNameTextField.text!.isEmpty{
        planNameFehlerLabel.isHidden = false
    }
    else{
        newPlan.name = planNameTextField.text!
        newPlan.creationDate = NSDate() as Date
        let selectedExcercises = loadSelectedExcercises()
        if selectedExcercises != nil{
            for var i in (0..<selectedExcercises!.count){
                excerciseCopies.append(selectedExcercises![i])
                do{
                    try realm.write{
                        realm.add(excerciseCopies[i])
                    }
                }
                catch{
                    print(error)
                }
            }
            try! realm.write{
                realm.add(newPlan)
            }
            for object in excerciseCopies {
                do{
                    try realm.write{
                        newPlan.excercises.append(object)
                }
                }
                catch{
                    print(error)
                }
            }
            performSegue(withIdentifier: "savePlan", sender: self)
        }
        else{
            uebungFehlerLabel.isHidden = false
        }
    }
}

Then I give the newPlan object to another ViewController:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier! == "savePlan" {
        let tct = segue.destination as! TrainingPlanConfTableViewController
        tct.plan = newPlan
    }
}

Now in the next class I want to show all exercise objects from the copied array in a tableView, but the tableView doesn't show anything:

class TrainingPlanConfTableViewController: UITableViewController {

//Properties
let realm = try! Realm()
var excercisesFromPlan: Results<Excercise>?{
    didSet{
        tableView.reloadData()
    }
}
var plan: TrainingPlan?

//Lifecycle
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    loadExcercisesFromPlan()
}

//Request
func loadExcercisesFromPlan(){
    if plan != nil{
        let predicate = NSPredicate(format: "trainingsplan = %@", plan!)
        excercisesFromPlan = realm.objects(Excercise.self).filter(predicate)
    }
}

//Tableview Funktionen
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if excercisesFromPlan != nil{
    return excercisesFromPlan!.count
}
    return 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: TrainingPlanConfTableViewCell = tableView.dequeueReusableCell(withIdentifier: "PlanConfCell") as! TrainingPlanConfTableViewCell
    let excercise = excercisesFromPlan![indexPath.row]
    cell.nameLabel.text = excercise.name
    return cell
}

}

I'm pretty new in Swift and I can't find the issue. Is the predicate seated wrong? I would be thankful for any help! If you need any more information, for example the data model etc, pls feel free to ask


Solution

  • Since you didn't include the code for your models, there's no way to know for sure if your predicate is correct or not.

    What I can tell is the TrainingPlan has a exercises property on it that you should use instead of querying for the same thing.

    You would just need to change excercisesFromPlan to be a List instead of a Results

    var excercisesFromPlan: List<Excercise>?
    

    and change loadExcercisesFromPlan to get it

    func loadExcercisesFromPlan() {
        excercisesFromPlan = plan?.excercises
    }