gpt4 book ai didi

ios - 一个实体的 NSManagedObjectContextObjectsDidChangeNotification

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:57:58 25 4
gpt4 key购买 nike

NSManagedObjectContextObjectsDidChangeNotification 是否有可能仅在特定实体更改时收到通知?

我想在我的联系信息或头像发生变化时更新我的​​ View ,但是使用 NSManagedObjectContextObjectsDidChangeNotification 每次数据库发生变化时我都会收到通知。

可以用 NSManagedObjectContextObjectsDidChangeNotification 做到这一点吗?

最佳答案

我认为不可能只针对特定实体触发。但是,该通知确实提供了有关更改了哪些对象的信息。通知包含一个字典 (userInfo),其中包含 3 个键:

  • NSDeletedObjectsKey - 所有已删除对象的数组
  • NSInsertedObjectsKey - 已添加/插入的所有对象的数组
  • NSUpdatedObjectsKey - 所有已修改对象的数组

您可以遍历这些数组的内容并确定您的特定对象是否已更改。粗略的概述如下:

- (void) handleObjectsChangedNotification:(NSNotification*) notification {
// Iterate over all of the deleted objects
for (NSManagedObject* object in notification.userInfo[NSDeletedObjectsKey]) {
}

// Iterate over all of the new objects
for (NSManagedObject* object in notification.userInfo[NSInsertedObjectsKey]) {
}

// Iterate over all of the modified objects
for (NSManagedObject* object in notification.userInfo[NSUpdatedObjectsKey]) {
}
}

关于ios - 一个实体的 NSManagedObjectContextObjectsDidChangeNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25923663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com