0

I'm new in Swift and I have a problem with a fetch request. The request is nil, so it doesn't show me the data I want. But I do the same before in the UebungenMuskelgruppenTableViewController and it works. Only the requests in the PlanMuskelgruppenViewController are nil, but I used the same entities. The boolean uebungForPlan is nil too, so the segue doesn't pass the variables to the UebungAnzeigenViewController and I don't know why. Here's my Code:

class UebungenMuskelgruppenTableViewController: UITableViewController {

var mgdContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

var uebungen = [Uebung]()

override func viewDidLoad() {
    super.viewDidLoad()

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let uvc = segue.destinationViewController as? UebungenAnzeigenViewController {
        if let identifier = segue.identifier{
            switch identifier{
            case "Brust":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Brust")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            case "Rücken":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Rücken")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            case "Beine":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Beine")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            case "Schultern":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Schultern")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            case "Bizeps":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Bizeps")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            case "Trizeps":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Trizeps")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            case "Nacken":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Nacken")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            case "Unterarme":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Unterarme")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
            default:
                break
            }
        }
    }
}

Here's the Code from the Viewcontroller, where the request are nil:

class PlanMuskelgruppenViewController: UIViewController {

var mgdContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

var uebungen = [Uebung]()

override func viewDidLoad() {
    super.viewDidLoad()

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let uvc = segue.destinationViewController as? UebungenAnzeigenViewController {
        if let identifier = segue.identifier{
            switch identifier{
            case "BrustPlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Brust")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            case "RückenPlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Rücken")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            case "BeinePlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Beine")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            case "SchulternPlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Schultern")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            case "BizepsPlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Bizeps")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            case "TrizepsPlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Trizeps")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            case "NackenPlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Nacken")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            case "UnterarmePlan":
                let request = NSFetchRequest(entityName: "Uebung")

                request.predicate = NSPredicate(format: "muskel == %@", "Unterarme")
                do{
                    try uebungen = mgdContext.executeFetchRequest(request) as! [Uebung]
                }
                catch{
                    print("Abfrage fehlgeschlagen!")
                }
                uvc.request2 = request
                uvc.uebungForPlan = true
            default:
                break
            }
        }
    }
}

}

And here's the Code, where the requests were executed:

class UebungenAnzeigenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var mgdContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

@IBOutlet weak var hinzufuegenButtonOutlet: UIBarButtonItem!
@IBOutlet weak var tableView: UITableView!
var uebungen = [Uebung](){
    didSet{
        self.tableView.reloadData()
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    loadUebungen()
    setupTableView()
    print("boolean: \(uebungForPlan)")
}

var request2 = NSFetchRequest?()

var uebungForPlan: Bool?


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("UebungenCell", forIndexPath: indexPath)
    let uebung = uebungen[indexPath.row]
    cell.textLabel!.text = uebung.name!
    cell.textLabel?.textAlignment = .Center
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return uebungen.count
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if uebungForPlan == true{
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let uebungen = self.uebungen[indexPath.row]
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        if uebungen.ausgewaehlt == true{
            uebungen.ausgewaehlt = false
        }
        else{
            uebungen.ausgewaehlt = true
        }
        if uebungen.ausgewaehlt == true{
            cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
        }
        else{
            cell?.accessoryType = UITableViewCellAccessoryType.None
        }
    }
}

func loadUebungen(){
    if request2 != nil{
    do{
        try uebungen = mgdContext.executeFetchRequest(request2!) as! [Uebung]
    }
    catch{
        print(error)
    }

    }}

func setupTableView(){
    if uebungForPlan == true{
        hinzufuegenButtonOutlet.title = "Zu Plan hinzufügen"

    }
}

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    if identifier == "neueUebung"{
        if uebungForPlan == true{
            return false
        }
        else{
            return true
        }
    }
    return false
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if uebungForPlan == nil{
    if segue.identifier == "neueUebung" {
        let ctrl = segue.destinationViewController as! UebungHinzufuegenViewController
        ctrl.saveDelegate = {
            (newUebung, newMuskelgruppe) in

            let neueUebung = NSEntityDescription.insertNewObjectForEntityForName("Uebung", inManagedObjectContext: self.mgdContext) as! Uebung
            neueUebung.name = newUebung
            neueUebung.muskel = newMuskelgruppe

            do{
                try self.mgdContext.save()
            }
            catch{
                print("speichern fehlgeschlagen")
            }
            self.loadUebungen()
            self.navigationController?.popViewControllerAnimated(true)
        }
    }
}
}

}

Sorry for my bad English and thanks for any help, I tried lots of thing like clear the project, reset the emulator and a few days ago i did a complete rebuild of the app, nothing works.

  • Why are you executing the fetch request inside `prepareForSegue`? Is the query failed ("Abfrage fehlgeschlagen!") message getting printed ever? Instead of `var request2 = NSFetchRequest?()` you should just use `var request2:NSFetchRequest?` Not sure if that's the issue though. Have you tried using breakpoints / `print` statements to figure out what lines of code are/aren't being executed? – shim May 03 '16 at 14:05
  • I deleted the executeFetchRequest method in the prepareForSegue method, but it doesn't fix the problem. I have a ViewController with exactly the same code in the prepareForSegue and this ViewController pass the data right. The message "Abfrage fehlgeschlagen" is never printed, and if I use breakpoints, the whole code works except the executeFetchRequest method in the loadUebungen method.. The print method says that the boolean and the fetch are nil. – Marcel Spindler May 03 '16 at 20:06
  • Did you try my other suggestion? By the way, your cases in your switch statement are all basically the same. Why not just use `request.predicate = NSPredicate(format: "muskel == %@", identifier)` ? – shim May 03 '16 at 21:25
  • Yes, I also changed the NSFetchRequest?() to NSFetchRequest?, but it doesn't changed anything. That's a great idea, thanks! – Marcel Spindler May 03 '16 at 23:36
  • I setted a breakpoint in the prepareforsegue method and found out that the method is never called. So this is the reason why this doesn't work, but I don't know why.. – Marcel Spindler May 04 '16 at 15:20
  • I forgot to assign the tableviewcontroller class to the tableviewcontroller in the storyboard -.- know it works! Thanks for the help! – Marcel Spindler May 04 '16 at 15:29

0 Answers0