NSURLConnection didReceiveData未被调用 [英] NSURLConnection didReceiveData not called

查看:92
本文介绍了NSURLConnection didReceiveData未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了大量的消息,再次说同样的事情:当你使用NSURLConnection时,委托方法不被称为。我理解Apple的文档是不完整的并且引用了弃用的方法,这是一种耻辱,但我似乎无法找到解决方案。

I've read through tons of messages saying the same thing all over again : when you use a NSURLConnection, delegate methods are not called. I understand that Apple's doc are incomplete and reference deprecated methods, which is a shame, but I can't seem to find a solution.

请求的代码在那里:

// Create request
NSURL *urlObj = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urlObj cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];

if (![NSURLConnection canHandleRequest:request]) {
    NSLog(@"Can't handle request...");
    return;
}

// Start connection
dispatch_async(dispatch_get_main_queue(), ^{
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; // Edited
});

...代表方法的代码在这里:

...and code for the delegate methods is here :

- (void) connection:(NSURLConnection *)_connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Receiving response: %@, status %d", [(NSHTTPURLResponse*)response allHeaderFields], [(NSHTTPURLResponse*) response statusCode]);
    self.data = [NSMutableData data];
}

- (void) connection:(NSURLConnection *)_connection didFailWithError:(NSError *)error {
    NSLog(@"Connection failed: %@", error);
    [self _finish];
}

- (void) connection:(NSURLConnection *)_connection didReceiveData:(NSData *)_data {
    [data appendData:_data];
}

- (void)connectionDidFinishDownloading:(NSURLConnection *)_connection destinationURL:(NSURL *) destinationURL {
    NSLog(@"Connection done!");
    [self _finish];
}

这里没有很多错误检查,但我已经确定了一些事情:

There's not a lot of error checking here, but I've made sure of a few things :


  • 无论发生什么, didReceiveData 从不调用,所以我没有得到任何数据

  • ...但数据被转移(我使用 tcpdump 检查)

  • ...并成功调用其他方法。

  • 如果我使用 NSURLConnectionDownloadDelegate 而不是 NSURLConnectionDataDelegate ,一切正常但我无法保留下载的文件(这是一个已知的错误)

  • 在内存管理不良之前,请求未被解除分配

  • 如果我在互联网上的某个地方使用标准HTML页面作为我的URL,则没有任何变化

  • 请求从主队列开始

  • Whatever happens, didReceiveData is never called, so I don't get any data
  • ...but the data is transfered (I checked using tcpdump)
  • ...and the other methods are called successfully.
  • If I use the NSURLConnectionDownloadDelegate instead of NSURLConnectionDataDelegate, everything works but I can't get a hold on the downloaded file (this is a known bug)
  • The request is not deallocated before completion by bad memory management
  • Nothing changes if I use a standard HTML page somewhere on the internet as my URL
  • The request is kicked off from the main queue

我不想使用第三方库,因为最终这些请求将被包括在图书馆中我自己,我想尽量减少依赖。如果必须的话,我会直接使用 CFNetwork ,但这对你知道什么来说会非常痛苦。

I don't want to use a third-party library, as, ultimately, these requests are to be included in a library of my own, and I'd like to minimize the dependencies. If I have to, I'll use CFNetwork directly, but it will be a huge pain in the you-know-what.

如果您有任何想法,那将会有很大帮助。谢谢!

If you have any idea, it would help greatly. Thanks!

推荐答案

我喜欢使用sendAsynchronousRequest方法..连接过程中信息较少,但代码更清洁。

I like to use the sendAsynchronousRequest method.. there's less information during the connection, but the code is a lot cleaner.

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
        if (data){
            //do something with data
        }
        else if (error)
            NSLog(@"%@",error);
    }];

来自Apple:


默认情况下,在创建时,
默认模式下的当前线程上会安排连接。如果使用
initWithRequest创建连接:delegate:startImmediately:方法并为
提供no的startImmediately参数,则可以在启动它之前在
不同的运行循环或模式上安排连接启动方法。
您可以在多个运行循环和模式上安排连接,或者在
上安排多个模式下的相同运行循环。

By default, a connection is scheduled on the current thread in the default mode when it is created. If you create a connection with the initWithRequest:delegate:startImmediately: method and provide NO for the startImmediately parameter, you can schedule the connection on a different run loop or mode before starting it with the start method. You can schedule a connection on multiple run loops and modes, or on the same run loop in multiple modes.

除非有理由在[NSRunLoop currentRunLoop]中明确运行它,否则
你可以删除这两行:

Unless there is a reason to explicitly run it in [NSRunLoop currentRunLoop], you can remove these two lines:

[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[connection start];

或将模式更改为NSDefaultRunLoopMode

or change the mode to NSDefaultRunLoopMode

这篇关于NSURLConnection didReceiveData未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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