【发布时间】:2010-10-16 22:06:58
【问题描述】:
另见:
我花了一天时间写这段代码,谁能告诉我这里出了什么问题?
WSHelper 是继承自 NSObject,我什至尝试过 NSDocument 和 NSObjectController 等等……
-(void) loadUrl: (NSString*) urlStr{
url = [[NSURL alloc] initWithString:urlStr];
request = [NSURLRequest requestWithURL:url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 60.0];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if(connection)
{
receivedData = [[NSMutableData data] retain];
//[connection start];
}
else
{ display error etc... }
NSApplication * app = [NSApplication sharedApplication];
[app runModalForWindow: waitWindow];// <-- this is the problem...
}
-(void)connection: (NSURLConnection*)connection didReceiveData:(NSData*)data{
progressText = @"Receiving Data...";
[receivedData appendData:data];
}
-(void)connection: (NSURLConnection *)connection didFailWithError:(NSError *)error{
progressText = @"Error...";
NSAlert * alert = [[NSAlert alloc] init];
[alert setMessageText:[error localizedDescription]];
[alert runModal];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
progressText = @"Done...";
pData = [[NSData alloc] initWithData:receivedData];
[self hideWindow];
}
代码不会做任何事情,它根本没有进展。我什至尝试使用/不使用 startImmediately:YES 但没有运气!!!,这是在主窗口中执行的,因此即使线程及其运行循环也成功运行。
我尝试调用同步请求,它工作正常!!但我需要异步解决方案。
我已经在项目中添加了 CoreServices.Framework,还有什么我应该添加到项目中的吗?任何编译器设置?还是我必须先初始化任何东西才能使用 NSURLConnection?
任何在其自己的 NSRunLoop、Objective-C 和 MAC 开发的不同线程上运行 NSURLConnection 的解决方案在文档中的任何地方都没有示例代码,这使得所有内容都难以编码。
【问题讨论】:
-
sendSynchronousRequest:returningResponse:error: 不起作用,因为它是一个类方法,并且异常表明您已将其发送到实例。
-
谢谢,我会尝试修复它,但无论如何我正在寻找异步解决方案,我很累......
-
我了解您正在寻找异步解决方案,但正如文档所说,同步方法在内部使用异步代码。它的结果将告诉我们更多关于你的代码失败的地方。
标签: objective-c cocoa asynchronous nsurlconnection