为什么我的NSOperationQueue在iOS 4.0中无法正常运行? [英] Why is my NSOperationQueue not behaving correctly in iOS 4.0?

查看:55
本文介绍了为什么我的NSOperationQueue在iOS 4.0中无法正常运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在iPhone OS 3.0中已经在我的iPhone应用程序中使用过NSOperationQueue,但是现在在iOS 4.0中,代码无法正常工作.它只能正常运行一次,并且在所有后续调用中均无法正常运行. iOS 4.0中的NSOperationQueue是否进行了更改?

I have used NSOperationQueue in my iPhone app before in iPhone OS 3.0, but now in iOS 4.0 the code is not working properly. It runs properly only once and on all subsequent calls, it doesnt work. Have there been changes in NSOperationQueue in iOS 4.0?

相关代码如下:

   - (void) starteffectFunction {

        NSOperationQueue *queue = [NSOperationQueue new];
        NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(starteffectProcessing)
                                                                                  object:nil];

        [queue addOperation:operation];
        [operation release];
        [queue release];
        [spinner startAnimating];
}

    -(void) starteffectProcessing{
    some code executes. code snippet. A 
    ......
    this code is note supposed to execute before A completes. But this executes before A.
    }

推荐答案

您正在创建NSOperationQueue,向其添加操作,然后释放队列.这不是NSOperationQueue设计的工作方式. NSOperationQueue应该持久存在,并根据需要向其添加操作.

You are creating an NSOperationQueue, adding an operation to it, then releasing the queue. This is not how NSOperationQueues were designed to work. An NSOperationQueue is supposed to persist, with you adding operations to it as necessary.

这可能是失败的,因为您要在NSOperationQueue有机会触发操作线程之前取消分配NSOperationQueue.也许在较早的OS版本上,由于某些时间上的古怪,它才能够做到这一点.

This is probably failing because you are deallocating the NSOperationQueue before it has a chance to fire off a thread for your operation. Perhaps on the older OS versions it was just able to do this due to some timing quirk.

我建议您在第一次需要效果处理队列时或在控制器对象的初始化中分配效果处理队列,然后将该队列作为控制器对象的实例变量.该队列将与控制器对象同时释放,但是您可能希望当时取消所有当前操作,并使用NSOperationQueue的–waitUntilAllOperationsAreFinished方法来确保要在释放之前完成所有工作.

I recommend allocating the effect processing queue when you first need it, or in the initialization of your controller object, then keeping that queue around as an instance variable of your controller object. This queue would be deallocated at the same time as your controller object, but you will probably want to cancel all current operations at that time and use NSOperationQueue's –waitUntilAllOperationsAreFinished method to make sure that you are completing all work before deallocation.

这篇关于为什么我的NSOperationQueue在iOS 4.0中无法正常运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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