首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“消息已收到但未处理”KVO

“消息已收到但未处理”KVO
EN

Stack Overflow用户
提问于 2012-09-05 03:52:45
回答 2查看 13.6K关注 0票数 23

运行项目时,我在输出窗口中看到以下内容:

代码语言:javascript
运行
复制
An -observeValueForKeyPath:ofObject:change:context: message was received but not handled.
Key path: connection.messageQueue
Observed object: <Client: 0x10011ddb0>
...

你明白了吧。问题是,我不知道为什么会发生这种事。然而,看起来一切都运行得很好。以下是导致问题的代码:

代码语言:javascript
运行
复制
-(id) initWithIdentifier:(NSString*)ident andClient:(Client*)chatClient {
    if(![super initWithNibName:@"ChatView" bundle:nil]) {
        return nil;
    }
    [self setTitle: ident];
    client = chatClient;
    [self setIdentifier:ident];

    inContext = false;

    [client addObserver:self forKeyPath:@"connection.messageQueue" options:NSKeyValueObservingOptionNew context:NULL];
    return self;
}

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSAttributedString* rar = [[NSAttributedString alloc] initWithString:@"test"];
    [[textView textStorage] appendAttributedString:rar];
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

我想我应该展示与此相关的所有代码。这个类只有几个方法,所以这就是您需要看到的全部内容。我甚至没有使用更改,当KVO事件被触发时,我只是在"test“上拨打。

堆栈跟踪变得非常大,非常快,因为消息一直都在进来。然而,看起来一切都很正常。

有什么线索吗?

EN

回答 2

Stack Overflow用户

发布于 2012-09-05 03:55:25

如果您处理通知,则不应调用[super observeValueForKeyPath:ofObject:change:context:]。您应该只为您自己不处理的通知调用此方法。

票数 49
EN

Stack Overflow用户

发布于 2021-06-03 15:37:41

确保你只对你不感兴趣的东西打电话给超级用户。

代码语言:javascript
运行
复制
class Layer: CALayer {
    
    private let key1 = "transport"
    private let key2 = "position"
        
    override init() {
        super.init()
        addObserver(self, forKeyPath: key1, options: [], context: nil)
        addObserver(self, forKeyPath: key2, options: [], context: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == key1 || keyPath == key2 {
            
        } else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }
    
    deinit {
        removeObserver(self, forKeyPath: key1)
        removeObserver(self, forKeyPath: key2)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12270429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档