为什么我的 NSOperationQueue 在 iOS 4.0 中行为不正确?

发布于 2024-09-28 18:08:39 字数 824 浏览 0 评论 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.
    }

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?

The relevant code is as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2024-10-05 18:08:39

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

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

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

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文