【问题标题】:NSOperation inside NSOperationQueue not being executedNSOperationQueue 中的 NSOperation 没有被执行
【发布时间】:2012-03-26 17:25:56
【问题描述】:

我真的需要帮助。在这一点上我很绝望。

我有 NSOperation,当添加到 NSOperationQueue 时没有被触发。 我添加了一些日志来查看 NSOperation 状态,结果如下:

Queue operations count = 1
Queue isSuspended = 0
Operation isCancelled? = 0
Operation isConcurrent? = 0
Operation isFinished? = 0
Operation isExecuted? = 0
Operation isReady? = 1
Operation dependencies? = 0

代码很简单。没什么特别的。

       LoadingConflictEvents_iPad *loadingEvents = [[LoadingConflictEvents_iPad alloc] initWithNibName:@"LoadingConflictEvents_iPad" bundle:[NSBundle mainBundle]];

       loadingEvents.modalPresentationStyle = UIModalPresentationFormSheet;
       loadingEvents.conflictOpDelegate = self;

       [self presentModalViewController:loadingEvents animated:NO];

       [loadingEvents release];

       ConflictEventOperation *operation = [[ConflictEventOperation alloc] initWithParameters:wiLr.formNumber pWI_ID:wiLr.wi_id];

       [queue addOperation:operation];           

       NSLog(@"Queue operations count = %d",[queue operationCount]);
       NSLog(@"Queue isSuspended = %d",[queue isSuspended]);
       NSLog(@"Operation isCancelled? = %d",[operation isCancelled]);
       NSLog(@"Operation isConcurrent? = %d",[operation isConcurrent]);
       NSLog(@"Operation isFinished? = %d",[operation isFinished]);
       NSLog(@"Operation isExecuted? = %d",[operation isExecuting]);
       NSLog(@"Operation isReady? = %d",[operation isReady]);
       NSLog(@"Operation dependencies? = %d",[[operation dependencies] count]);


       [operation release];

现在我的操作在 main 方法上做了很多事情,但问题从来没有被调用过。 main 永远不会被执行。 最奇怪的事情(相信我,我还没有疯……)。如果我在任何 NSLog 行或创建操作中放置一个断点,主方法将被调用,一切都会完美运行。

这已经运行了很长时间了。我最近一直在做一些改变,显然有些事情搞砸了。其中一项更改是将设备升级到 iOS 5.1 SDK (iPad)。

添加一些东西,我有这个应用程序的 iPhone (iOS 5.1) 版本,它使用相同的 NSOperation 对象。区别仅在于 UI,一切正常。

哦,这只会在实际设备上失败。在模拟器中一切正常。

任何帮助将不胜感激。

问候,

【问题讨论】:

    标签: ios nsoperation nsoperationqueue


    【解决方案1】:

    如果操作是并发的,则需要实现-start,而不是-main。

    【讨论】:

    • 操作不是并发的。谢谢!
    【解决方案2】:

    我有同样的问题,但我的解决方案不一样,所以我想我也会在这里为像我这样的未来人提供我的答案。

    我的问题是在我的 NSOperation 子类中,我有:

    @property CGPoint start;
    

    在合成时创建方法:

    -(CGPoint)start
    

    覆盖 NSOperation 的 -(void)start;方法,它是非并发 NSOperation 的标志,并且阻止了 -(void)start 中发生的所有常规事情的发生,从而完全阻止了 -(void)main 方法被调用。

    一旦我将我的属性重命名为 start 以外的其他名称,它就可以正常工作。

    【讨论】:

      【解决方案3】:

      好的,我终于解决了这个问题。

      我遇到的问题是由于 NSOperation 一直在后台运行(但在不同的队列中)。此操作阻塞了要执行的任何其他线程 (NSOperation)。这仅在 iPad 1 中发生,因为它不是双核。

      我的 NSOperation 在 main 方法上做了这样的事情:

      - (void)main 
      {
          while (![self isCancelled]) {
              //Do stuff
          }
      }
      

      NSOperation 一直在做这件事

      我真傻,我没有给操作系统时间来处理其他线程,所以添加睡眠就可以了

      - (void)main
      {
          while (![self isCancelled]) {
              [NSThread sleepForTimeInterval:0.5];
              //Do Stuff
          }
      }
      

      睡眠使操作系统有机会与其他线程一起工作。

      为什么设置断点会起作用? ...调试器停止了所有线程,因为断点在我的另一个 NSOperation 中,所以该线程被执行。

      【讨论】:

      • 那一定是一个启发性的调试会话。对我来说,是的。
      【解决方案4】:

      好的,这个问题只发生在 iPad 1 设备上,应用程序使用 SDK 5.1 编译。 我尝试使用带有 iOS 4.2.1 的 iPad 1 和带有 iOS 5.1 的 iPad 1。两者都给出了相同的问题。

      确保这在使用 SDK 4.3 编译的应用程序的两台 iPad 上都有效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多