NSOperationQueue内部的NSOperation未被执行

Mar*_*cia 5 nsoperation nsoperationqueue ios

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

我有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
Run Code Online (Sandbox Code Playgroud)

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

       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];
Run Code Online (Sandbox Code Playgroud)

现在我的操作在main方法上做了很多事情,但问题永远不会被调用.主要从未执行过.最奇怪的事情(相信我,我并不疯狂......)如果我在任何NSLog行或创建操作中设置断点,将调用main方法并且一切都将完美地工作.

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

为了添加内容,我使用了相同NSOperation对象的此应用程序的iPhone(iOS 5.1)版本.区别在于UI,一切正常.

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

任何帮助将非常感激.

问候,

jsd*_*jsd 5

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


Mar*_*cia 4

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

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

我的 NSOperation 在 main 方法上做了类似的事情:

- (void)main 
{
    while (![self isCancelled]) {
        //Do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

NSOperation 一直在不断地这样做

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

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

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

为什么设置断点就能完成工作?...调试器停止了所有线程,并且因为断点位于我的另一个 NSOperation 中,所以该线程被执行。