NS操作和键值观察

编程入门 行业动态 更新时间:2024-10-27 12:26:06
本文介绍了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操作和键值观察

本文发布于:2023-10-13 17:08:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:键值   操作   NS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!