NS操作和键值观察 [英] NSoperation and key value observing

查看:43
本文介绍了NS操作和键值观察的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 MyOperation 对象(从 NSOperation 继承)并添加到 NSOperationQueue.然后我在 MyOperation 上做 KVO.我正在使用这种方法

I am creating a MyOperation object (inherited from NSOperation) and add to a NSOperationQueue. Then I am doing KVO on MyOperation. I am using this method

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;

如果完成,则从 MyOperation 获取值.在这个方法中,我使用了另一个类中的便捷方法来获取一些其他数据.

to get a value from MyOperation if it is finished. In this method I am using a convenience method from an other class to get some other data.

可能是observeValue...方法中的同步问题?

Maybe here synchronization problems in the observeValue... method?

推荐答案

我没有足够的声誉来评论或反对,但我会反对 dannywartnaby 的回答.NSOperation 在其操作中本质上使用了 KVO;必须实现 isFinished 和 isExecuted 作为处理的一部分,例如是 100% KVO 并且是操作所需的与客户端/队列的合同的一部分.因此建议应该避免这些是不准确的.NSOperation 本身使用 KVO,因此采用 dannywartnaby 的前提表明 NSOperation 的实现本身存在缺陷,事实并非如此.

I havent enough reputation to comment or down-vote but i would down-vote dannywartnaby's answer. NSOperation uses KVO intrinsically in it's operation; having to implement isFinished and isExecuted as part of the processing, for example are 100% KVO and are part of an operation's required contract with a client/queue. So advising that these should be avoided is inaccurate; NSOperation uses KVO itself so adopting dannywartnaby's premise would indicate that the implementation of NSOperation is in itself flawed, which is definitely not the case.

诚然,开发人员必须注意正确使用 KVO,尽管我认为对 NSOperation 进行子类化是一项可能留给更有经验的 Objective-C 程序员的重要任务.有人可能会争辩说,对于操作所承担的工作类型来说,通知的粒度太粗了,尽管并非总是如此,但我认为我们在建议实践时必须小心,以确保错误的消息不会作为最佳实践传递.Danny,您能否详细说明为什么在使用 NSOperation 或线程时不建议使用 KVO?很多 NSOperation 实现的例子都使用了 KVO...

Admittedly the developer must take care to use KVO properly, although i would argue that subclassing NSOperation is a non-trivial task probably left for the more experienced Objective-C programmer. Notifications, one could argue, are too coarse grained for the kind of work undertaken by an operation, although not always, i think we must be careful when advising on practices to ensure that the wrong message isnt passed as best practice. Danny, would you care to elaborate on why using KVO is not advised when using NSOperation or threads? many examples of NSOperation implementation use KVO...

这篇关于NS操作和键值观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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