是否会调用 NSManagedObjectContextDidSaveNotification 来进行瞬时属性更新?
我似乎无法得到明确的答案:当您更改瞬态属性,然后调用保存时,是否应该触发 NSManagedObjectContextDidSaveNotification ?在我的通知侦听器中,如何过滤掉来自瞬态属性更改的这些通知?
这就是我想要做的:我想在主线程中加载联系人列表,完成后,我想从地址簿中读取后台线程中的图像并将它们附加到联系人。从表面上看,这工作得很好:从联系人实体加载后,我使用调度队列循环所有联系人,在地址簿中找到他们的图像,并将它们保存在联系人的“contactImage”属性中(这是暂时的) 。然后,调度队列成功地重新加载表格视图(在主线程上),并且图像显示在联系人旁边。 问题是,如果我对联系人执行任何操作,即使在一个托管对象上调用“保存”(例如,我删除其中一个联系人),也会为所有联系人调用 NSManagedObjectContextDidSaveNotification。我发现这是因为 contactImage 属性在...评论“self.contactImage = img;”之前已更改线使问题消失。这让我感到惊讶,因为我本以为只有非瞬态属性才会调用保存通知。
任何人都可以确认这是否是预期的行为?或者我做错了什么?如果是预期的,如何过滤掉 NSManagedObjectContextDidSaveNotification 侦听器中瞬态属性的更新?我需要在侦听器中进行一些后处理,并且我不想为瞬态属性更新不必要地执行此操作。我已经检查了 NSManagedObject 上的changedValues字典,但它似乎在侦听器内显示为空(因为我猜只有瞬态属性发生了变化)。
谢谢。
I can't seem to get a clear answer for this: when you change a transient property, and then call save, should the NSManagedObjectContextDidSaveNotification be triggered? In my notification listener, how can I filter out these notifications that are coming from changes in transient properties?
Here's what I'm trying to do: I want to load up a list of contacts in the main thread, and when it's done, I want to read the images in a background thread from the address book and attach them to the contacts. This works fine on the face of it: after loading from the Contacts entity, I use a dispatch queue to loop through all the contacts, find their image in the Address Book, and save them in Contact's "contactImage" property (which is transient). The dispatch queue then successfully reloads the tableview (on the main thread) and the images show up next to the contacts.
The problem is that if I do anything to the contact that invokes a "save" on even ONE of the managed objects (for e.g. I delete one of the contacts), the NSManagedObjectContextDidSaveNotification is invoked for ALL the contacts. I've found that this is because the contactImage property was changed before ... commenting that the "self.contactImage = img;" line makes the issue go away. This is surprising to me, since I would have thought that the save notification would only be called for non-transient properties.
Can anyone confirm if this is expected behavior? Or am I doing something wrong? If it's expected, how do you filter out the updates to transient properties in the NSManagedObjectContextDidSaveNotification listener? I need to do some post-processing in the listener, and I don' want to do it needlessly for transient property updates. I've checked the changedValues dictionary on the NSManagedObject, but it seems to show empty inside the listener (since only transient properties changed, I'm guessing).
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
昨天,
瞬态财产有一个关键特征——它们是受管理的。您可以轻松添加不受任何 NSManagedObject 管理的 ivars。如果您这样做,它们将不受 -save: 通知的约束。
一个相关的问题:为什么要使用瞬态 ivar?它们有一些专门的用途;它们主要用于触发整个模型的属性更新;即您所看到的行为。
第二个相关问题:为什么要在后台获取所有图像而不是从地址簿中延迟加载它们?对我来说,这看起来像是一个过早优化的例子。
安德鲁
Yesterday,
Transient properties have one key characteristic -- they are managed. You can easily add ivars that are not managed to any NSManagedObject. If you do so, they are not subject to -save: notifications.
A related question: Why are you using a transient ivar? They have some specialized uses; primarily, they are used to trigger property updates throughout the model; i.e. the behavior you are seeing.
A second related question: why are you background fetching all of the images instead of lazy loading them from the Address Book? This looks like a case of premature optimization to me.
Andrew