NSFetchedResultsController在修改管理对象时通知其委托人删除更改,并且从不通知插入或更新 [英] NSFetchedResultsController notifies its Delegate of delete changes when a managed object is modified, and never notifies for Insert or Update

查看:161
本文介绍了NSFetchedResultsController在修改管理对象时通知其委托人删除更改,并且从不通知插入或更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewController ,它是一个 NSFetchedResultsController 的委托。我的 NSFetchedResultsControllerDelegate 函数按照 Apple的文档,将表视图控制器作为提取的结果控制器的委托属性。



我也有一些视图控制器,它们显示在表视图的顶部,其中可以修改被管理对象。这些修改是在同一个受管对象上下文中完成的。在我的应用程序中只构建了一个受管对象上下文,它被全局访问。 (我已经在我的对象上下文构造中添加了一些 print 语句,以确保我不会在其他地方重新构建它。)



当我修改一个托管对象时,调用委托函数 controller:didChangeObject ,但是 NSFetchedResultsChangeType 总是 。删除。当我创建一个托管对象时,委托函数根本不触发。



但是,当我手动调用 performFetch c $ c>和 tableView.reloadData(),单元格恢复到正确的状态:删除的行回来,创建任何未插入的行。



结果是,删除对象的工作原理如期望(单元格被删除),但对对象的更新导致其单元格被删除,对象创建不触发单元格插入。



我试图创建一个这个行为的简单演示,但当我从一个空白应用程序重新创建情况,我没有看到这种行为。所以在我的应用程序中的东西导致这种奇怪的行为,但我不知道什么。任何想法?






额外信息



谓词和排序描述符的实际构造是通过几个不同的类完成的,但是通过 print(resultsController.fetchRequest.predicate) print(resultsController.fetchRequest.sortDescriptors)提供以下内容:


可选(readState ==2 OR readState ==1)



可选([(readState,ascending,compare :),(title,ascending,compare:)])


我在我的控制器:didChangeObject:方法中打印了一个语句,这只能通过 type.rawValue = 2 (即 .Delete )调用,只有当我修改对象时, c> 处理其 NSPredicate



如果 NSFetchedResultsController 具有如下谓词的整数和字符串之间的比较的获取请求:

  let predicate = NSPredicate格式:integerAttribute ==%@,String(1))

字符串是:


integerAttribute ==1



$ b b

在这种情况下,初始抓取工作正常:在抓取的结果控制器上调用函数 performFetch()返回 integerAttribute 等于 1 (其中 integerAttribute 的类型为然而, NSFetchedResultsControllerDelegate 的通知不能正常工作。

对受管对象的修改导致向委托人通知 NSFetchedResultsChangeType.Delete 更改。



要使所有这些奇怪消失,修正谓词格式字符串如下:

  let predicate = NSPredicate(format:integerAttribute ==%d,1)


I have a UITableViewController, which is a delegate for an NSFetchedResultsController. My NSFetchedResultsControllerDelegate functions are set up as per "Typical Use" in Apple's documentation, with the table view controller as the fetched result controller's delegate property.

I also have some view controllers which are presented on top of the table view where the managed objects can be modified. These modifications are done on the same managed object context. There is only one managed object context constructed in my application, and it is accessed globally. (I have put some print statements in my object context construction just to be sure I am not accidentally re-constructing it elsewhere.)

When I modify one of the managed objects, the delegate function controller:didChangeObject is called, but the NSFetchedResultsChangeType is always .Delete. When I create a managed object, the delegate function does not fire at all.

However, when I manually call call performFetch() and tableView.reloadData(), the cells are restored to the correct state: the removed row comes back, any not-inserted rows are created.

The result is that deleting an object works as expected (the cell is removed), but updates to an object cause its cell to be removed, and object creations do not trigger cell inserts.

I tried to create a simple demo of this behaviour, but when I re-created the situation from a blank application, I don't see this behaviour. So something within my application is causing this strange behaviour, but I can't figure out what. Any ideas?


Extra Info:

The actual construction of the predicate and sort descriptors are done over several different classes, but printing them via print(resultsController.fetchRequest.predicate) and print(resultsController.fetchRequest.sortDescriptors) gives the following:

Optional(readState == "2" OR readState == "1")

Optional([(readState, ascending, compare:), (title, ascending, compare:)])

I have put a print statement in my controller:didChangeObject: method, and I can see that this only gets called with type.rawValue = 2 (i.e. .Delete), and only when I modify objects, not when I create them.

解决方案

It's an inconsistency with how NSFetchedResultsController handles its NSPredicate.

If the NSFetchedResultsController is constructed with a fetch request which has a predicate which does a comparison between an integer and a string like follows:

let predicate = NSPredicate(format: "integerAttribute == %@", String(1))

this will lead to the predicate string being:

integerAttribute == "1"

When this is the case, initial fetches work fine: calling the function performFetch() on the fetched results controller returns all objects where the integerAttribute is equal to 1 (where integerAttribute is of type Int32).

However, the notifications to NSFetchedResultsControllerDelegate do not work fine. Modifications of managed objects result in the delegate being notified of a NSFetchedResultsChangeType.Delete change. Creations of managed objects do not invoke the delegate at all.

To make all this weirdness go away, fix the predicate format string as follows:

let predicate = NSPredicate(format: "integerAttribute == %d", 1)

这篇关于NSFetchedResultsController在修改管理对象时通知其委托人删除更改,并且从不通知插入或更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆