gpt4 book ai didi

ios - 以前发布的获取通知的解决方案(在 NSOperationQueue 上完成所有任务)对我不起作用

转载 作者:行者123 更新时间:2023-11-28 20:24:32 25 4
gpt4 key购买 nike

Get notification when NSOperationQueue finishes all tasks

我遇到的问题与上面帖子中@porneL 发布的问题相同。我尝试了@NickForge 发布的解决方案(获得 57 票),但我显然做错了,因为它对我不起作用。这是问题设置和我的实现:

我需要在启动一组 Web 服务操作之前启动微调器,并在它们完成后停止微调器。 Web 服务通过共享的 AFHTTPClient 实例(AFNetworking 包的一部分)调用,该实例将它们添加到其 NSOperationQueue

我在 ViewController 中设置了一个观察器,从中开始加载数据。这是使用上面帖子的答案吗?我的 VC 中的实现如下所示:

在我的 ViewController 的 init 方法中:

//add WelcomeVC as an observer for AFHTTPClient dataloadOps notifications
[[[MyCustomAFHTTPClient sharedClient] operationQueue] addObserver:self forKeyPath:@"DataLoaderEvent" options:0 context:NULL];

在我的 ViewController 的 observeValueForKeyPath 方法中:

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if( (object == [[MyCustomAFHTTPClient sharedClient] operationQueue])
&& [keyPath isEqualToString:@"DataLoaderEvent"]) {

if ( [[[MyCustomAFHTTPClient sharedClient] operationQueue] operationCount] == 0) {

NSLog(@"EUREKA!!! QUEUE IS EMPTY! DATALOAD HAS COMPLETED!");
}

}
}

ViewController 的 observeValueForKeyPath 方法永远不会被调用!

如果您能帮助我完成这项工作,我将不胜感激,这样我就可以完成微调器的实现了。

最佳答案

operationQueue 是否有一个名为 DataLoaderEvent 的属性?通常情况下,我们会监视 NSOperationQueue 的“operationCount”属性。

参见 http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/doc/uid/TP40004592

“NSOperationQueue 类符合键值编码 (KVC) 和键值观察 (KVO)。您可以根据需要观察这些属性以控制应用程序的其他部分。您可以观察的属性包括以下内容:

操作 - 只读属性

operationCount - 只读属性"

关于ios - 以前发布的获取通知的解决方案(在 NSOperationQueue 上完成所有任务)对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14470052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com