简体   繁体   English

使用NSMutableData iOS进行键值观察

[英]Key-Value Observing with NSMutableData iOS

In my model I have NSMutableData Object called data. 在我的模型中,我有称为数据的NSMutableData对象。 I want to observe it from another object and get a notification when data is added or removed. 我想从另一个对象观察它,并在添加或删除数据时收到通知。

I understand that this is a to-many relationship, so I can't use the normal KVO by first adding the observer with this code 我了解这是一对多的关系,因此我无法通过先向观察者添加此代码来使用普通的KVO

[object addObserver:self forKeyPath:@"data" options:NSKeyValueObservingOptionNew context:NULL];

and then implement the observeValueForKeyPath:ofObject:change:context: method to do something when the notification is send. 然后实现observeValueForKeyPath:ofObject:change:context:方法以在发送通知时执行某些操作。

I have read the Key-Value Observing Documentation and I found other posts about it. 我已经阅读了关键值观察文档,并且发现了有关它的其他文章。 Observing Changes to a mutable array using KVO vs. NSNotificationCenter & Observing an NSMutableArray for insertion/removal They all use NSMutableArray as example. 使用KVO和NSNotificationCenter 观察 对可变数组的更改观察NSMutableArray的插入/移除操作它们都以NSMutableArray为例。 Is it also possible for NSMutableData? NSMutableData也可以吗? I don't understand what I have to implement then. 我不明白那时我该执行什么。

Can someone tell me what I exactly have to implement in which class to get KVO for a NSMutableData object working? 有人可以告诉我为使NSMutableData对象正常工作而必须在哪个类中实现什么才能获得KVO? Or if this is not possible, another solution? 还是如果这不可能,另一种解决方案?

Many Thanks! 非常感谢!

There is a common misunderstanding with KVO: It is not possible to observe the inner state of an otherwise KVO compliant property. KVO有一个常见的误解:无法观察到其他KVO兼容属性的内部状态。

That's the case with the NSData: From the KVO point of view the data property of your object does not change, it's its state of the NSData that changes. NSData就是这种情况:从KVO的角度来看,对象的data属性不会更改,而是NSData的状态更改。 If NSMutableData would expose KVO compliant properties for its contents you could observe changes by observing a key path (like @"data.contents") but that's not the case. 如果NSMutableData为其内容公开KVO兼容属性,则可以通过观察键路径(例如@“ data.contents”)来观察更改,但事实并非如此。

KVO only works for KVC compliant properties that are also documented to be KVO compliant. KVO仅适用于KVC兼容属性,这些属性也已记录为KVO兼容。 Most Cocoa framework classes don't give that guarantee and it's a bug to observe the objects and their key paths. 大多数Cocoa框架类都不提供这种保证,并且观察对象及其关键路径是一个错误。

A solution for your case would be not to expose an NSMutableData as a public property but instead use an immutable data and add methods like appendData: to your custom class. 针对您的情况的解决方案不是将NSMutableData公开为公共属性,而是使用不可变数据并将诸如appendData:类的方法添加到自定义类。 That would give you the opportunity to do the changes to the data object in these methods and emit KVO notifications manually (using willChangeValueForKey:@"data" ... do the change ... didChangeValueForKey:@"data" ). 这将使您有机会在这些方法中对数据对象进行更改并手动发出KVO通知(使用willChangeValueForKey:@"data" ...进行更改... didChangeValueForKey:@"data" )。

KVO won't help you to observe internal changes of NSMutableData. KVO不会帮助您观察NSMutableData的内部变化。 In your example, you're observing only "data" property and you will get notification about changes only if your Model's data property is changed (eg you assign another instance of NSData). 在您的示例中,您仅观察“ data”属性,并且只有在更改Model的data属性(例如,分配另一个NSData实例)时,您才会收到有关更改的通知。

In your case, I would like to propose you to use notifications: Object, which is interested in data mutations subscribes to notification of given name, and Model posts this notification every time new data was appended to Model's data. 对于您的情况,我建议您使用通知:对数据突变感兴趣的对象订阅给定名称的通知,并且每当新数据追加到模型的数据时,模型都会发布此通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM