下面的URLConnection有什么问题? [英] What's wrong on following URLConnection?

查看:172
本文介绍了下面的URLConnection有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


Objective-C使用Cookie的异步Web请求

我花了一天的时间写这个代码,任何人都可以告诉我这里有什么错误?

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];
}

代码不会做任何事情,我甚至尝试它/没有startImmediately:YES但没有运气!!!,这是在主窗口中执行,所以即使线程和其运行循环正在成功运行。

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.

我试着调用同步请求,它是正常工作!但我需要async解决方案。

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?

任何解决方案运行NSURLConnection在不同的线程上自己的NSRunLoop,Objective-C和MAC开发没有样本

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]

删除 [连接开始] 。现在:


  • 您的应用程序是否通常正常运行运行循环?

  • 您可以执行网址请求的同步加载吗?

  • 在调试器中,您可以看到 url 是你期望的吗?它是什么?

  • 在收到任何委托消息之前,是否有可能释放WSHelper? NSURLConnection毕竟是异步的。

  • 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不需要做任何特殊操作,这是Foundation框架的一个简单的部分。无需特殊的编译器设置。使用前无需初始化。没有。请不要盲目地尝试像引入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 中运行,因此连接是

  • 您的代码的其他部分在连接上调用 -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 期望的运行循环。我会说,真的,你不想出现一个像这样的窗口,而运行一个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:委托方法。您想在这里检查服务器是否返回预期的状态代码。

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.

这篇关于下面的URLConnection有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆