菜鸟教程小白 发表于 2022-12-12 11:32:43

ios - 不调用 NSFetchedResultsControllerDelegate 委托(delegate)方法


                                            <p><p>这是我的代码:</p>

<pre><code>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(&#34;Controller will change&#34;)
    }

    func controllerDidChangeContent(controller: NSFetchedResultsController) {
      print(&#34;COntroller did changed&#34;)
    }

    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
      print(&#34;Controller did change section&#34;)
    }

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
      print(&#34;Controller did change object at indexpath&#34;)
    }
}


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)
}
</code></pre>

<p>当我添加数据时,不会调用 <code>FetchResultControllerDataProvider</code> 中的任何方法。但我不知道为什么。 </p>

<p>有人可以帮我吗?</p>

<p>编辑:</p>

<p>添加数据:</p>

<pre><code>func addTransaction() {
    self.context.performChanges {
      Transaction.insert(self.context, name: self.name.text!, amount: Double(self.amount.text!)!, type: self.currentTypeTag)
    }

public func saveOrRoleback() -&gt; Bool {

    do {
      try save()
      return true
    } catch {
      rollback()
      return false
    }
}

public func performChanges(block: () -&gt; ()) {

    performBlockAndWait {
      block()
      self.saveOrRoleback()
    }
}
</code></pre>

<p>所以这部分代码被调用了,并没有抛出错误。<br/>
但是我试图检查核心数据sql文件,它似乎是空的。所以没有添加数据。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>添加数据时,您需要检查委托(delegate)和 fetchedResultsController 对象是否仍然存在并正确填充。您可以尝试打印这些变量的值吗?</p>

<pre><code>self.fetchedResultController

self.fetchedResultController.delegate
</code></pre>

<p>您的<code>PersistentStore</code> 中添加数据时是否插入了数据?</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 不调用 NSFetchedResultsControllerDelegate 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37647629/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37647629/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 不调用 NSFetchedResultsControllerDelegate 委托(delegate)方法