如何检查NSOperationQueue是否已完成以及任何操作是否失败? [英] How to check if NSOperationQueue is finished and if any operation failed?

查看:110
本文介绍了如何检查NSOperationQueue是否已完成以及任何操作是否失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在后台解析一些XML文件,以使UI不会冻结.我必须检查两件事:

  • NSOperationQueue完成了吗?
  • NSOperation-解析是否失败?

我有一个子类化NSOperation的类,如果解析失败,则调用一个委托.队列中的操作只能同时进行一次.

我的问题是,我不能依靠这样的事实,即在我获得队列完成消息之前就执行了失败的消息.有时,在收到完成的消息之前,我不会收到失败的消息.然后,例如,我有以下命令:

操作1成功 行动2成功 OperationQueue完成 操作3失败

所有消息都发送到主线程.收到完成的消息后,我想继续执行代码,但前提是所有操作均成功.我该如何处理队列完成后调用委托消息的问题.

这是我代码的一部分:

//XMLOperation.m
- (void)main {    
    NSLog(@"Operation started");

    if ([self parseXML]) {
            [self performSelectorOnMainThread:@selector(finishedXMLParsing) withObject:nil waitUntilDone:NO];
        } else {
            [self performSelectorOnMainThread:@selector(failedXMLParsing) withObject:nil waitUntilDone:NO];
        }
    }
    NSLog(@"Operation finished");
}


//StartController.m
[self.xmlParseQueue addObserver:self forKeyPath:@"operations" options:0 context:NULL];

...

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
                         change:(NSDictionary *)change context:(void *)context
{
    if (object == self.xmlParseQueue && [keyPath isEqualToString:@"operations"]) {
        if ([self.xmlParseQueue.operations count] == 0) {
            // Do something here when your queue has completed
            NSLog(@"queue has completed");
        }
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object 
                               change:change context:context];
    }
}

解决方案

之所以会发生这种情况,是因为在您完成/失败的通知时,不一定要在主线程上传递队列的operations属性的KVO通知. /p>

您还应确保在主线程上也执行完成通知,以便定义通知的顺序.

I'm trying to parse some XML files in the background so that the UI doesn't freeze. I have to check two things:

  • NSOperationQueue is finished?
  • NSOperation - parsing did fail?

I have a class that subclasses NSOperation and a delegate is called if the parsing failed. Operations in the queue are limited to one simultaneously.

My problem is that I can't rely on the fact that the failed message is executed before I get the queue did finish message. Sometimes I don't get a failed message before I get the finished message. Then, for example, I have this order:

Operation 1 Successful Operation 2 Successful OperationQueue finished Operation 3 Failed

All messages are sent to the main thread. After I get the finished message I want to proceed in my code, but only if all operations were successful. How can I handle the problem that the delegate message is called after my queue is finished.

This are some parts of my code:

//XMLOperation.m
- (void)main {    
    NSLog(@"Operation started");

    if ([self parseXML]) {
            [self performSelectorOnMainThread:@selector(finishedXMLParsing) withObject:nil waitUntilDone:NO];
        } else {
            [self performSelectorOnMainThread:@selector(failedXMLParsing) withObject:nil waitUntilDone:NO];
        }
    }
    NSLog(@"Operation finished");
}


//StartController.m
[self.xmlParseQueue addObserver:self forKeyPath:@"operations" options:0 context:NULL];

...

- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object 
                         change:(NSDictionary *)change context:(void *)context
{
    if (object == self.xmlParseQueue && [keyPath isEqualToString:@"operations"]) {
        if ([self.xmlParseQueue.operations count] == 0) {
            // Do something here when your queue has completed
            NSLog(@"queue has completed");
        }
    }
    else {
        [super observeValueForKeyPath:keyPath ofObject:object 
                               change:change context:context];
    }
}

解决方案

This probably happens because your KVO notification for the operations property of the queue is not necessarily delivered on the main thread while your finished/failed notifications are.

You should ensure that the completion notification is performed on the main thread as well, so that the order of your notifications is defined.

这篇关于如何检查NSOperationQueue是否已完成以及任何操作是否失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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