0

我之前在 iPhone OS 3.0 中的 iPhone 应用程序中使用过 NSOperationQueue,但现在在 iOS 4.0 中代码无法正常工作。它只在所有后续调用中正常运行一次,它不起作用。iOS 4.0 中的 NSOperationQueue 有变化吗?

相关代码如下:

   - (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.
    }
4

1 回答 1

2

您正在创建一个 NSOperationQueue,向其中添加操作,然后释放队列。这不是 NSOperationQueues 的设计方式。NSOperationQueue 应该持续存在,您可以根据需要向其添加操作。

这可能会失败,因为您在 NSOperationQueue 有机会为您的操作触发线程之前释放了它。也许在较旧的操作系统版本上,由于一些时间问题,它只是能够做到这一点。

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

于 2010-10-21T15:25:28.777 回答