- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我对委托(delegate)的实现:
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
ItemListTableViewController.logger.log("begin updates")
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
ItemListTableViewController.logger.log("end updates")
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
ItemListTableViewController.logger.log(type.rawValue, "\(indexPath) -> \(newIndexPath)")
switch type {
case .delete:
self.tableView.deleteRows(at: [indexPath!], with: .automatic)
case .insert:
self.tableView.insertRows(at: [newIndexPath!], with: .automatic)
case .move:
self.tableView.moveRow(at: indexPath!, to: newIndexPath!)
case .update:
self.tableView.reloadRows(at: [indexPath!], with: .automatic)
}
}
这是我得到的日志:
ItemListTableViewController >>> "perform insert start"
ItemListTableViewController >>> "begin updates"
ItemListTableViewController >>> 1 "nil -> Optional([0, 0])"
ItemListTableViewController >>> 4 "Optional([0, 1]) -> Optional([0, 2])"
ItemListTableViewController >>> "end updates"
ItemListTableViewController >>> "perform insert end"
我正在尝试通过调用 context.insert(item)
将一个新项目插入到上下文中,并根据日志的第 3 行插入一个新项目。并且 Controller 根据第 4 行移动一些项目。但是 NSFetchedResultsChangeType
中原始值“4”的类型应该是 update
而不是 move
。
我也测试过其他情况,当我需要更新一个项目时,它给了我一个 move
类型。
我对 update
和 move
的含义有误吗?或者这是一个错误?
我错了。这是一个错误的代码。
我正在使用 Xcode 8.3.1
最佳答案
感谢@Joe Rose,我明白这是如何工作的。我不处理可能同时插入和移动的任务,所以我没有使用您的解决方案。
我对update
和move
的理解是错误的。所以我再次阅读了苹果的文档。它说:
Changes are reported with the following heuristics:
On add and remove operations, only the added/removed object is reported.
It’s assumed that all objects that come after the affected object are also moved, but these moves are not reported.
A move is reported when the changed attribute on the object is one of the sort descriptors used in the fetch request.
An update of the object is assumed in this case, but no separate update message is sent to the delegate.
An update is reported when an object’s state changes, but the changed attributes aren’t part of the sort keys.
因此,当报告move
时,也会报告update
。那时,获取的对象更新了,但 tableView 没有更新,所以我需要使用 indexPath
来定位单元格,并使用 newIndexPath
来获取正确的对象。
这是我的最终解决方案,目前表现良好:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
ItemListTableViewController.logger.log(type.rawValue, "\(indexPath) -> \(newIndexPath)")
switch type {
case .insert:
self.tableView.insertRows(at: [newIndexPath!], with: .automatic)
case .delete:
self.tableView.deleteRows(at: [indexPath!], with: .automatic)
case .move:
self.tableView.moveRow(at: indexPath!, to: newIndexPath!)
fallthrough
case .update:
self.configure(cell: tableView.cellForRow(at: indexPath!) as! ItemInfoCell, at: newIndexPath!)
}
}
func configure(cell: ItemInfoCell, at indexPath: IndexPath) {
let item = fetchController.object(at: indexPath)
cell.item = item
}
如果有任何问题,请告诉我。
关于swift - NSFetchedResultsControllerDelegate 返回错误的 NSFetchedResultsChangeType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43486500/
我无法弄清楚为什么当我向底层数据存储添加数据时,NSFetchedResultsControllerDelegate 方法没有触发。如果我重新启动 iPhone 应用程序,数据会立即显示。 我已经子类
我是 iOS (Swift) 的新手,正在尝试了解 NSFetchedResultsControllerDelegate 机制。我特别不明白的是 NSFetchedResultsChangeType
我的 viewWillAppear 方法中有这个 [RPCoreData getFetchedControllerForCategoryDiscoverDelegate:self completio
NSFetchedResultsControllerDelegate 的文档提供了以下示例代码 - (void)controller:(NSFetchedResultsController *)con
我想在 CollectionViewController 中使用 NSFetchedResultsControllerRelegate。因此,我只是更改了 CollectionView 的 Table
我在 Xcode 收到以下警告,但不知道原因。欢迎任何帮助。 Assigning to 'id' from incompatible type 'CollapsableTableViewViewCo
据我了解,NSFetchedResultsController 负责将数据从托管对象上下文同步到 TableView Controller 。代表在这里的作用是什么?我阅读了苹果文档并没有得到它。请简
我正在尝试为实现 NSFetchedResultsControllerDelegate protocol 的 View Controller 编写单元测试。 .实现的第一个测试(在此 View Con
问候 Stack 的公民,当尝试覆盖“controllerDidChangeContent”时,我得到“方法不会覆盖其父类(super class)中的任何方法”。该 stub 由自动完成处理程序生成
在阅读了大量 Core Data 框架的文档和示例之后,我仍然不太明白。发生的情况是我有点理解示例代码或文档的部分内容,但通常无法将其放入更大的图片中。第二次读同样的代码,和第一次一样,还是需要很多时
这是我对委托(delegate)的实现: func controllerWillChangeContent(_ controller: NSFetchedResultsController) {
我正在使用一些在 UITableView 中实现 NSFetchedResultsControllerDelegate 的样板代码。当我执行远离 TableView 的 segue 时,将调用此委托(
我正在结合 NSFetchedResultsController 实现 UITableViewController。当 UITableViewController 实例化时,基于用户在前一个 Cont
前提:我有一个符合NSFetchedResultsControllerDelegate的UITableViewController。我还有一个获取结果 Controller 和托管对象上下文作为 Co
这是我的代码: class FetchResultControllerDataProvider: NSObject, NSFetchedResultsControllerDelegate {
我目前正在开发一个基于 CoreData 的 iPhone 应用程序,它的 map View 将从 生成其注释。 NSFetchedResultsController . map View 的想法是它
我有一个示例应用程序,它有一个带有单个属性的 NSManagedObject(我们称类为 CustomObject)。 该应用程序有一个简单的用户界面,由一个带有 UICollectionView 的
我正在尝试在 UITableView 中使用 NSFetchedResultsController。但是由于某些原因,当我添加一个新条目时,NSFetchedResultsControllerDele
我在 .h 中定义了一个 UITableViewController,如下所示: @interface PartsSearchViewController : UITableViewControlle
我喜欢使用 CoreData 根据数据库中的创建日期填充记录,所以我使用 NSFetchedResultsControllerDelegate 来获得良好的功能,正如我添加的那样: @interfac
我是一名优秀的程序员,十分优秀!