过滤 NSFetchedResultsController 结果 swift 2
Posted
技术标签:
【中文标题】过滤 NSFetchedResultsController 结果 swift 2【英文标题】:filter NSFetchedResultsController result swift 2 【发布时间】:2016-04-16 18:04:23 【问题描述】:我想在 tableview 上自动更新..为此我使用了NSFetchedResultsController
as
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】:-
您必须使用
self.fetchedResultsController
,这是您的数据源,而不是filterfetchResultsController
:没有连接。
您必须执行 performFetch() 来执行更改的谓词
这对我有用:
@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()
【讨论】:
这是解决我的问题的最佳方式吗? i 认为,在您的代码中缺少 self.fetchedResultsController.performFetch() 并且您没有使用正确的 fetchedResultsController。在我的应用程序中,这种方式有效。但这是最好的方式吗?我永远不会为此而争辩,你永远可以学习新的方法。 ;-)以上是关于过滤 NSFetchedResultsController 结果 swift 2的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 3 中难以配置 NSFetchedResultsController
为啥 beginUpdates/endUpdates 会重置表视图位置以及如何阻止它这样做?