什么是错在下面的URLConnection?
Objective-C异步Web请求与饼干
我花了一天写这篇code和任何人都可以告诉我什么是错在这里?
I spent a day writing this code and can anyone tell me what is wrong here?
WSHelper从NSObject的继承,我甚至尝试NSDocument和NSObjectController和一切..
WSHelper is inherited from NSObject, I even tried NSDocument and NSObjectController and everything..
-(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];
}
在code只是不会做任何事情,它不进步可言。我甚至尝试了/不startImmediately:是的,但没有运气!!!,这是在主窗口所以即使是线程中执行和它的运行循环运行成功
The code just wont do anything, it doesnt progress at all. I even tried it with/without startImmediately:YES but no luck !!!, this is executed in main window so even the thread and its run loop is running successfully.
我打过电话同步请求,并且其正常工作!但我需要异步的解决方案。
I tried calling synchronous request, and it is working correctly !! But I need async solution.
我在项目中加入CoreServices.Framework,有什么更多的,我应该加入到这个项目?任何编译器设置?或者我必须初始化任何东西之前,我可以使用NSURLConnection的?
I have added CoreServices.Framework in project, is there anything more I should be adding to the project? any compiler settings? Or do i have to initialize anything before I can use NSURLConnection?
任何解决方案来对自己的NSRunLoop不同的线程,Objective-C的运行NSURLConnection的MAC和发展有没有样品code在文档中的任何地方,让一切都这么难code。
Any solution to run NSURLConnection on different thread on its own NSRunLoop, Objective-C and MAC Development has no sample code anywhere in documentation that makes everything so difficult to code.
首先,你正在做启动连接太复杂。更改为:
Firstly you're making starting the connection too complicated. Change to:
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]
删除 [连接开始]
。现在:
- 是您的应用程序运行的绝对运行循环是否正常? NSURLConnection的需要这个工作。
- 您能够执行URL请求的同步负荷?
- 在调试器中,你能看到
网址
是你希望它是什么?这是什么? - 是否有可能接收到的任何代表消息之前,你重新分配WSHelper? NSURLConnection的是asynchoronous毕竟。
- Is your app definitely running the run loop normally? NSURLConnection requires this to work.
- Are you able to perform a synchronous load of the URL request?
- In the debugger, can you see that
url
is what you expect it to be? What is it? - Is it possible that you're deallocating WSHelper before any delegate messages are received? NSURLConnection is asynchoronous after all.
一个人并不需要做什么特别的使用NSURLConnection的,它的基础框架的一个简单的一部分。不需要特殊的编译器设置。在使用前没有初始化。没有。请不要盲目地开始尝试像引进CoreServices.Framework的东西。
One does not need to do anything special to use NSURLConnection, it's a straightforward part of the Foundation framework. No special compiler settings required. No initialization before use. Nothing. Please don't start blindly trying stuff like bringing in CoreServices.Framework.
由于同步发送请求的作品,一定有什么问题与您的异步方面的处理。它可以是:
As sending the request synchronously works, there must be something wrong with your handling of the asynchronous aspect. It could be:
- 的runloop没有在
NSDefaultRunLoopMode
运行,这样的连接是无法安排自己。 - 您$ C $ C的其他部分正在调用
-cancel
在连接上它有一个机会来加载之前。 - 您正在设法释放连接的时候有机会先加载。
- The runloop is not running in
NSDefaultRunLoopMode
so the connection is unable to schedule itself. - Some other part of your code is calling
-cancel
on the connection before it has a chance to load. - You are managing to deallocate the connection before it has a chance to load.
嗯,其实我刚刚意识到发生了什么事情。您呼叫:
Ah, in fact I've just realised what's going on. You are calling:
-[NSApp runModalForWindow:]
阅读一下这种方法确实的说明。这不是在运行运行的循环如 NSURLConnection的
的期望。我想说的是真的,你不想被presenting一个窗口,很喜欢这个运行时为它的URL连接。
Read the description of what this method does. It's not running the run loop like NSURLConnection
expects. I'd say that really, you don't want to be presenting a window quite like this while running a URL connection for it.
我也建议你实施 -connection:didReceiveResponse:
委托方法了。要检查这里的服务器返回预期的状态code。
I'd also suggest that you implement the -connection:didReceiveResponse:
delegate method too. You want to check here that the server is returning the expected status code.