这是我的代码:
class FetchResultControllerDataProvider: NSObject, NSFetchedResultsControllerDelegate {
private let fetchedResultController: NSFetchedResultsController
private var delegate: TodayViewController!
init(fetchedResultController: NSFetchedResultsController, delegate: TodayViewController) {
self.fetchedResultController = fetchedResultController
self.delegate = delegate
super.init()
self.fetchedResultController.delegate = self
try! self.fetchedResultController.performFetch()
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
print("Controller will change")
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
print("COntroller did changed")
}
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
print("Controller did change section")
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
print("Controller did change object at indexpath")
}
}
func loadData() {
let request = NSFetchRequest(entityName: Transaction.entityName)
request.predicate = Transaction.defaultPredicate
request.sortDescriptors = []
request.returnsObjectsAsFaults = false
request.fetchBatchSize = 20
let frc = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
let dataProvider = FetchResultControllerDataProvider(fetchedResultController: frc, delegate: self)
dataSource = TableViewDataSource(tableView: self.tableView, dataProvider: dataProvider, delegate: self)
}
当我添加数据时,不会调用 FetchResultControllerDataProvider
中的任何方法。但我不知道为什么。
有人可以帮我吗?
编辑:
添加数据:
func addTransaction() {
self.context.performChanges {
Transaction.insert(self.context, name: self.name.text!, amount: Double(self.amount.text!)!, type: self.currentTypeTag)
}
public func saveOrRoleback() -> Bool {
do {
try save()
return true
} catch {
rollback()
return false
}
}
public func performChanges(block: () -> ()) {
performBlockAndWait {
block()
self.saveOrRoleback()
}
}
所以这部分代码被调用了,并没有抛出错误。
但是我试图检查核心数据sql文件,它似乎是空的。所以没有添加数据。
添加数据时,您需要检查委托(delegate)和 fetchedResultsController 对象是否仍然存在并正确填充。您可以尝试打印这些变量的值吗?
self.fetchedResultController
self.fetchedResultController.delegate
您的PersistentStore
中添加数据时是否插入了数据?
关于ios - 不调用 NSFetchedResultsControllerDelegate 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37647629/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |