Iphone NSURLConnection和NSURLConnectionLegate在不同的线程上工作
正如标题中所描述的,我做了一些类似这样的连接。但是我发现委托方法无法运行。有人知道怎么处理吗 编辑: 我的委托在主线程上工作,而NSURLConnection在操作队列上工作。现在的情况是:NSURLConnection工作正常,但委托不会运行 编辑2: 我使用了类方法[NSURLConnection connectionWithRequest:delegate:] 这是我的主要队列Iphone NSURLConnection和NSURLConnectionLegate在不同的线程上工作,iphone,ios,nsurlconnection,nsurlconnectiondelegate,Iphone,Ios,Nsurlconnection,Nsurlconnectiondelegate,正如标题中所描述的,我做了一些类似这样的连接。但是我发现委托方法无法运行。有人知道怎么处理吗 编辑: 我的委托在主线程上工作,而NSURLConnection在操作队列上工作。现在的情况是:NSURLConnection工作正常,但委托不会运行 编辑2: 我使用了类方法[NSURLConnection connectionWithRequest:delegate:] 这是我的主要队列 NSOperationQueue *queuePhoto = [[NSOperationQueue alloc
NSOperationQueue *queuePhoto = [[NSOperationQueue alloc] init];
NSInvocationOperation *invocationOperationPhotos = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(transferToServerAddimages:) object:arrayOfASection];
// [invocationOperationPhotos addObserver:self forKeyPath:@"isExecuting" options:0 context:invocationOperationPhotos];
// [invocationOperationPhotos addObserver:self forKeyPath:@"isCancelled" options:0 context:invocationOperationPhotos];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(<#selector#>) name:@"isExecuting" object:nil];
[invocationOperationPhotos setQueuePriority:NSOperationQueuePriorityHigh];
[queuePhoto addOperation:invocationOperationPhotos];
[mutableArrayPhotoQueue addObject:queuePhoto];
[invocationOperationPhotos release];
[queuePhoto release];
}
这是我的代表:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[[AppDelegate sharedAppDelegate] mArrayOfFailedConnection] addObject:connection];
[connection cancel];
connection = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"response: %@", response);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connection: %@", connection);
}
实际上,当您将操作的选择器设置为“transferToServerAddimages”时,这意味着当此函数完全执行时,您的操作将完成,而且因为NSUrlConnection在启动它的同一线程上调用它的委托,所以该线程(OperationQueue)很可能在响应到达之前完成。如果确实要使用NSOperationQueue,那么更好的函数是NSUrlConnection的“sendAsynchronousRequest:queue:completionHandler:”。委托是什么,
NSOperation
?一种可能是您的操作正在调用-[connectionWithRequest:delegate:
并退出,这将导致操作完成并被删除。在操作的解除锁定中放置一个NSLog
,查看其是否正在运行
假设所有操作都是
NSURLConnection
transactions,则不需要将事务包装在NSOperation
中以获得并发性,因为这些事务已经是异步的。但是,如果您确实想使用NSOperation
(出于协调等目的),请将其设置为“并发”操作,并将其放在主队列中。这些类型比较复杂,但也有一些例子。如果你的线程没有运行循环,NSURLConnection将无法工作。你是否添加了这一行[connection startConnection]@Kevin Ballard,正如刚才编辑的,委托在主线程上,无法工作,不是NSURLConnection。@Arundhati,我使用了类方法[NSURLConnection connectionWithRequest:delegate://code>@Anna您必须在具有运行循环的线程上启动连接。然后将在该线程上调用委托方法。连接IO本身总是发生在由基金会管理的另一个线程上。
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[[AppDelegate sharedAppDelegate] mArrayOfFailedConnection] addObject:connection];
[connection cancel];
connection = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"response: %@", response);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connection: %@", connection);
}