在iOS5中创建许多下载对象时,NSURLConnection不会接收数据 [英] NSURLConnection doesn't receive data when creating many downloading objects in iOS5

查看:84
本文介绍了在iOS5中创建许多下载对象时,NSURLConnection不会接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在SOF上搜索这个问题好几天了,我还没有找到解决方案(说同样的问题)。

I have been searching for this problem on the SOF for several days and I still have not found the solution (say the same problem) yet.

我是制作和应用程序在URL列表中同时下载5个图像(每个图像在不同的服务器上)。

I'm making and app that downloads 5 images simultaneously in an URL list (each image is on a different server).

我有一个 ImageDownloader 类子类 NSOperation 并实现 NSURLConnectionDataDelegate

I have an ImageDownloader class subclasses NSOperation and implements the NSURLConnectionDataDelegate.

这样我就可以在 ViewController中的 operationQueue 中添加一个 ImageDownloader 的实例,它将在 operationQueue 下的单独线程中运行。将下载程序添加到 operationQueue 的行位于:

So that I can add an instance of ImageDownloader to an operationQueue in the ViewController and it will run in a separate thread under the operationQueue. The line that add the downloader to the operationQueue is here:

downloader = [[ImageDownloader alloc] init];
[downloader downloadImageWithURL:[controller.URList objectForKey:[NSString stringWithFormat:@"%d",downloadIndex]] queue:queue andTag:downloadIndex + 100]; //my custom initialize
downloader.delegate = self;
[queue addOperation:downloader]; //use the addOperation method

iOS6 中的一切正常但是搞砸了在 iOS5 (我的测试设备上为5.0,我的SDK上为5.1),它只是通过执行方法 didReceiveResponse 没有收到任何响应或数据和 didReceiveData 根本没有(这两种方法没有跳入)。

Everything works fine in iOS6 but messed up in iOS5 (5.0 on my test device and 5.1 on my SDK), it just doesn't receive any response nor data by performing the methods didReceiveResponse and didReceiveData at all (these 2 methods are not jumped in).

超过超时后,runloop跳转到 didFailWithError 方法,程序停止运行。

After the timeout was exceeded, the runloop jumps into didFailWithError method and the program stalls.

据我所知,这意味着runloop仍然正常运行?

As I understand, this means the runloop still runs right?

我试图打印错误我得到的只是:请求超时

I tried to print out the error and all I got is: The request timed out.

当我将下载实例的数量减少到2时,它会运行,但不会运行> = 3个下载实例。

When I reduce the number of downloading instances to 2 then it runs, but not with >=3 downloading instances.

还有一个信息是我的网络连接确实限制了连接数。但它在iOS6中运行良好,为什么它在iOS5上不起作用?

One more information is that my network connection does limit the number of connection. But it work fine in iOS6, why it just doesn't work on iOS5?

在下载应用程序时,我仍然可以在模拟器中加载网页。

I can still load the web in the simulator while the app is downloading.

那么是什么问题是这个,我怎么能克服这个问题?

So what kind of problem is this and how can I get over this problem?

提前致谢。

* 更新: * 因为有很多课程而且问题还没有被清楚地检测到,我将在这里分享整个项目。您可以直接从这里下载:
DownloadingImage

推荐答案

我刚刚弄清楚我的问题在哪里,但不是很明白为什么。

I've just figured out where my problem is, but not really understand why.

在我的 ImageDownloader 类中,我使用 done 和<$ c $设置了一个runloop c> currentRunLoop 变量。
在main方法中,我有一个while循环来强制 currentRunLoop 运行。

In my ImageDownloader class, I set up a runloop with done and currentRunLoop variables. In the main method, I have a while loop for forcing the currentRunLoop run.

当我删除那些runLoop的东西,该应用程序在iOS6和iOS5上运行顺畅。

As I remove those "runLoop" stuffs, the app runs smoothly on both iOS6 and iOS5.

所以更改整个 ImageDownloader.m 使用这些线然后它可以工作(我注释掉了一些无用的(比较有害的)线):

So change the entire ImageDownloader.m with these lines then it works (I commented out some useless (say harmful) lines):

//
//  ImageLoader.m
//  DownloadImagesTableView
//
//  Created by Viet Ta Quoc on 6/25/13.
//  Copyright (c) 2013 Viet Ta Quoc. All rights reserved.
//

#import "ImageDownloader.h"

@implementation ImageDownloader
@synthesize downloadData,delegate,queue,done,customTag;
NSRunLoop *currentRunLoop;

-(void)downloadImageWithURL:(NSString *)imageUrl queue:(NSOperationQueue*)opQueue andTag:(int)tag{
    self.customTag= tag;
    self.queue = opQueue;
//    self.done = NO;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    [connection start];
//    currentRunLoop = [NSRunLoop currentRunLoop];
    NSLog(@"Start downloading image %d...",customTag);
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"Received response...");
    downloadData=[[NSMutableData alloc] initWithLength:0];
    expectedDataLength=[response expectedContentLength];
    NSLog(@"Image %d size: %lld kb",customTag,[response expectedContentLength]/1024);
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    float receivedLenght = [data length];
    receivedDataLength=(receivedDataLength+receivedLenght);
    float progress=(float)receivedDataLength/(float)expectedDataLength;
    [delegate updateProgess:progress andIndex:[NSIndexPath indexPathForRow:customTag-100 inSection:0]];
    [self.downloadData appendData:data];
//    NSLog(@"Percentage of data received of tag %d: %f %%",self.customTag,progress*100);
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    [delegate finishedDownloadingImage:downloadData andTag:customTag];
//    done = YES;
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Network Connection Failed?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
//    NSLog(@"%@",[error debugDescription]);
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    [alert show];
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    NSLog(@"Got here *(*&(**&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&");
}

-(void)main{
//    do{
////        NSLog(@"Running....1");
//        [currentRunLoop runUntilDate:[NSDate distantFuture]];
//        //        [currentRunLoop run];
//    } while (!done);
//    [currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
@end

谢谢你们的支持。

======== ================================================== ========================

P / s:对于任何对此问题感兴趣的人,我在这里更新我的整个解决方案: DownloadImage_Final

P/s: for anyone who interested in this problem, I update here my entire solution: DownloadImage_Final

这篇关于在iOS5中创建许多下载对象时,NSURLConnection不会接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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