我有一个NSOperationQueue,它包含2个NSOperations,并通过将setMaxConcurrentOperationCount设置为1来依次执行它们。
其中一个操作是一个标准的非并发操作(只是一个main方法),它同步地从web (当然是在单独的操作线程上)检索一些数据。另一个操作是并发操作,因为我需要使用一些必须异步运行的代码。
问题是,我发现只有先将并发操作添加到队列中才能工作。如果它是在任何非并发操作之后出现的,那么奇怪的是,start方法会被调用很好,但是在该方法结束之后,我已经设置了我的连接来回调一个方法,它从来没有这样做过。之后将不再执行队列中的其他操作。就好像它在start方法返回后挂起,并且没有调用任何url连接的回调!
如果我的并发操作首先放在队列中,那么所有操作都能正常工作,异步回调工作并在完成后执行后续操作。我一点也不明白!
您可以在下面看到我的并发NSOperation的测试代码,我很确定它是可靠的。
任何帮助都将不胜感激!
主线程观察:
我刚刚发现,如果并发操作首先出现在队列上,那么在主线程上调用[start]方法。但是,如果它不是队列中的第一个(如果它是在并发或非并发之后),那么主线程上不会调用[start]方法。这似乎很重要,因为它符合我的问题的模式。这是什么原因?
并发NSOperation代码:
@interface ConcurrentOperation : NSOperation {
BOOL executing;
BOOL finished;
}
- (void)beginOperation;
- (void)completeOperation;
@end
@implementation ConcurrentOperation
- (void)beginOperation {
@try {
// Test async request
NSURLRequest *r = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]];
NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:r delegate:self];
[r release];
} @catch(NSException * e) {
// Do not rethrow exceptions.
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"Finished loading... %@", connection);
[self completeOperation];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Finished with error... %@", error);
[self completeOperation];
}
- (void)dealloc {
[super dealloc];
}
- (id)init {
if (self = [super init]) {
// Set Flags
executing = NO;
finished = NO;
}
return self;
}
- (void)start {
// Main thread? This seems to be an important point
NSLog(@"%@ on main thread", ([NSThread isMainThread] ? @"Is" : @"Not"));
// Check for cancellation
if ([self isCancelled]) {
[self completeOperation];
return;
}
// Executing
[self willChangeValueForKey:@"isExecuting"];
executing = YES;
[self didChangeValueForKey:@"isExecuting"];
// Begin
[self beginOperation];
}
// Complete Operation and Mark as Finished
- (void)completeOperation {
BOOL oldExecuting = executing;
BOOL oldFinished = finished;
if (oldExecuting) [self willChangeValueForKey:@"isExecuting"];
if (!oldFinished) [self willChangeValueForKey:@"isFinished"];
executing = NO;
finished = YES;
if (oldExecuting) [self didChangeValueForKey:@"isExecuting"];
if (!oldFinished) [self didChangeValueForKey:@"isFinished"];
}
// Operation State
- (BOOL)isConcurrent { return YES; }
- (BOOL)isExecuting { return executing; }
- (BOOL)isFinished { return finished; }
@end排队码
// Setup Queue
myQueue = [[NSOperationQueue alloc] init];
[myQueue setMaxConcurrentOperationCount:1];
// Non Concurrent Op
NonConcurrentOperation *op1 = [[NonConcurrentOperation alloc] init];
[myQueue addOperation:op1];
[op1 release];
// Concurrent Op
ConcurrentOperation *op2 = [[ConcurrentOperation alloc] init];
[myQueue addOperation:op2];
[op2 release];发布于 2009-10-29 13:37:39
我发现问题出在哪里了!
Dribin的这两篇无价文章非常详细地描述了并发操作,以及雪豹和iPhone SDK在异步调用需要运行循环的东西时引入的问题。
http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/ http://www.dribin.org/dave/blog/archives/2009/09/13/snowy_concurrent_operations/
感谢克里斯·苏特为我指明了正确的方向!
它的关键是确保我们调用主线程的start方法:
- (void)start {
if (![NSThread isMainThread]) { // Dave Dribin is a legend!
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
return;
}
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
// Start asynchronous API
}发布于 2009-10-29 11:25:11
您的问题很可能与NSURLConnection有关。NSURLConnection依赖于运行某个模式的运行循环(通常只是默认模式)。
对于您的问题,有许多解决方案:
上使它成为一个非并发操作。
如果你能逃脱它,做第4或#5。
发布于 2009-10-29 09:54:57
我没有注意到,也没有看到任何提到addDependency:的地方,这似乎是让操作按正确顺序执行的先决条件。
简而言之,第二个操作取决于第一个操作。
https://stackoverflow.com/questions/1642444
复制相似问题