So I'm fairly new to Core Data and KVO, but I have an NSManagedObject subclass that is successfully observing its own to-many relationship. The problem is, on observed changes, I want to iterate through only the set of objects that were added or removed. Is there some way to access these items directly? Or must I do something relatively inefficient like:
所以我对Core Data和KVO相当新,但是我有一个NSManagedObject子类,它成功地观察了它自己的to-many关系。问题是,在观察到的变化时,我想只迭代添加或删除的一组对象。有没有办法直接访问这些项目?或者我必须做一些相对低效的事情:
NSSet* newSet = (NSSet*)[change objectForKey:NSKeyValueChangeNewKey];
NSSet* oldSet = (NSSet*)[change objectForKey:NSKeyValueChangeOldKey];
NSMutableSet* changedValues = [[NSMutableSet alloc] initWithSet:newSet];
[changedValues minusSet:oldSet];
I feel like you should be able to avoid this because in these messages...
我觉得你应该能够避免这种情况,因为在这些消息中......
[self willChangeValueForKey:forSetMutation:usingObjects:];
[self didChangeValueForKey:forSetMutation:usingObjects:];
you're handing it the added/removed objects! Perhaps knowledge of what happens to these objects would be helpful?
你正在处理添加/删除的对象!也许知道这些物体会发生什么会有所帮助?
2 个解决方案
#1
5
Have you actually examined the contents of the "old" and "new" values provided by the KV observation? When I observe a change in a mutable set triggered by didChangeValueForKey:forSetMutation:usingObjects:
, the change dictionary value for NSKeyValueChangeNewKey holds only any added objects, while the value for NSKeyValueChangeOldKey holds only any removed objects, so you shouldn't have to manually figure out what has changed. However, an observation triggered by didChangeValue:forKey:
will give you the entire old collection for NSKeyValueChangeOldKey and the entire new collection for NSKeyValueChangeNewKey, even if they have identical contents.
您是否真的检查了KV观察提供的“旧”和“新”值的内容?当我观察到由didChangeValueForKey:forSetMutation:usingObjects:触发的可变集的更改时,NSKeyValueChangeNewKey的更改字典值仅保存任何添加的对象,而NSKeyValueChangeOldKey的值仅保存任何已删除的对象,因此您不必手动计算发生了什么变化。但是,由didChangeValue:forKey:触发的观察将为NSKeyValueChangeOldKey提供整个旧集合,并为NSKeyValueChangeNewKey提供整个新集合,即使它们具有相同的内容。
#2
2
When you register to observe an object, include the NSKeyValueObservingOptionNew option (and NSKeyValueObservingOptionOld too, if you want).
注册观察对象时,请包含NSKeyValueObservingOptionNew选项(如果需要,还包括NSKeyValueObservingOptionOld)。
#1
5
Have you actually examined the contents of the "old" and "new" values provided by the KV observation? When I observe a change in a mutable set triggered by didChangeValueForKey:forSetMutation:usingObjects:
, the change dictionary value for NSKeyValueChangeNewKey holds only any added objects, while the value for NSKeyValueChangeOldKey holds only any removed objects, so you shouldn't have to manually figure out what has changed. However, an observation triggered by didChangeValue:forKey:
will give you the entire old collection for NSKeyValueChangeOldKey and the entire new collection for NSKeyValueChangeNewKey, even if they have identical contents.
您是否真的检查了KV观察提供的“旧”和“新”值的内容?当我观察到由didChangeValueForKey:forSetMutation:usingObjects:触发的可变集的更改时,NSKeyValueChangeNewKey的更改字典值仅保存任何添加的对象,而NSKeyValueChangeOldKey的值仅保存任何已删除的对象,因此您不必手动计算发生了什么变化。但是,由didChangeValue:forKey:触发的观察将为NSKeyValueChangeOldKey提供整个旧集合,并为NSKeyValueChangeNewKey提供整个新集合,即使它们具有相同的内容。
#2
2
When you register to observe an object, include the NSKeyValueObservingOptionNew option (and NSKeyValueObservingOptionOld too, if you want).
注册观察对象时,请包含NSKeyValueObservingOptionNew选项(如果需要,还包括NSKeyValueObservingOptionOld)。