0

Okay so I am having an issue with my fetch results controller. The issue seems to be that when asking it to pull data from another table an exception occurs where it seems to be trying to convert one table into the other. Why is this?

var fetchedResultsController: NSFetchedResultsController<Task> {
        if _fetchedResultsController != nil {
            return _fetchedResultsController! as! 
NSFetchedResultsController<Task>
        }

        let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest()

        // Set the batch size to a suitable number.
        fetchRequest.fetchBatchSize = 20

        // Edit the sort key as appropriate.
        let sortDescriptor = NSSortDescriptor(key: "coursework", ascending: 
true)

        fetchRequest.sortDescriptors = [sortDescriptor]

        let currentCoursework = self.detailItem
        if(self.detailItem != nil){
            let predicate = NSPredicate(format: "moduleCoursework", currentCoursework!)
            fetchRequest.predicate = predicate
        }

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
        let aaFetchedResultsController = 
NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: 
self.managedObjectContext, 
sectionNameKeyPath:#keyPath(Coursework.modulename), cacheName: "Master")
        aaFetchedResultsController.delegate = self
        _fetchedResultsController = aaFetchedResultsController

        do {
            try _fetchedResultsController!.performFetch()
        } catch {
        /* Replace this implementation with code to handle the error 
appropriately.
         fatalError() causes the application to generate a crash log and 
terminate. You should not use this function in a shipping application, 
although it may be useful during development.*/
            let nserror = error as NSError
            fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
        }

        return _fetchedResultsController! as! 
NSFetchedResultsController<Task>
    }
    var _fetchedResultsController: NSFetchedResultsController<Coursework>? = 
nil
Joel
  • 1
  • Please show `self.detailItem`. In which line is the error? – shallowThought May 03 '17 at 16:18
  • I suspect it's because your computed variable, `fetchedResultsController` is of type `NSFetchedResultsController`, whereas the backing variable, `_fetchedResultsController` is of type `NSFetchedResultsController`. Try changing the latter to `...`. – pbasdf May 03 '17 at 16:31

0 Answers0