过滤NSFetchedResultsController结果swift 2

时间:2016-04-16 18:04:24

标签: ios swift uitableview core-data nsfetchedresultscontroller

我想在tableview上自动更新。因为我使用NSFetchedResultsController作为

lazy var fetchedResultsController: NSFetchedResultsController = {
    // Initialize Fetch Request
    let fetchRequest = NSFetchRequest(entityName: "Student")

    // Add Sort Descriptors
    let sortDescriptor = NSSortDescriptor(key: "grade", ascending: true)
    fetchRequest.sortDescriptors = [sortDescriptor]

    // Initialize Fetched Results Controller
    let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)

    // Configure Fetched Results Controller
    fetchedResultsController.delegate = self

    return fetchedResultsController
}()

但是我必须在用户选择加载后过滤结果为..

  @IBAction func onSegmentValueChanged(sender: UISegmentedControl)
    {
        if sender.selectedSegmentIndex == 0{

            //filter by grade
            let fetchRequest = NSFetchRequest(entityName: "Student")
            let sortDescriptor = NSSortDescriptor(key: "grade", ascending: true)
            fetchRequest.sortDescriptors = [sortDescriptor]
            let filterfetchResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
            let predicate = NSPredicate(format: "address = %@","Russia")
            filterfetchResultsController.fetchRequest.predicate = predicate
            self.tableView.reloadData()

        }else{

            let fetchRequest = NSFetchRequest(entityName: "Student")
            let sortDescriptor = NSSortDescriptor(key: "grade", ascending: true)
            fetchRequest.sortDescriptors = [sortDescriptor]
            let filterfetchResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
            let predicate = NSPredicate(format: "grade = %d",10)
            filterfetchResultsController.fetchRequest.predicate = predicate
            self.tableView.reloadData()


        }

    }

然而,重新加载tableView但数据未被过滤。我是否解决了这个问题?This是我正在处理的演示项目。或者我应该使用正常方式传递结果并重新加载tableView正常的方法来解决这个问题?

1 个答案:

答案 0 :(得分:6)

  1. 您必须使用self.fetchedResultsController,这是您的数据源,而不是filterfetchResultsController:没有连接。
  2. 您必须执行performFetch()才能执行更改的谓词
  3. 这对我有用:

    @IBAction func onSegmentValueChanged(sender: UISegmentedControl)
    {
        if sender.selectedSegmentIndex == 1{
    
            //filter by address
            let predicate = NSPredicate(format: "address == %@","Russia")
            self.fetchedResultsController.fetchRequest.predicate = predicate
    
        }else{
    
            let predicate = NSPredicate(format: "grade = %d",10)
            self.fetchedResultsController.fetchRequest.predicate = predicate
    
        }
    
        do {
            try self.fetchedResultsController.performFetch()
        } catch {
            let fetchError = error as NSError
            print("\(fetchError), \(fetchError.userInfo)")
        }
        self.tableView.reloadData()
    
    }