Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLConnection very long downloading

I'm facing problem with downloading 5 MB file, it taking more then 2 minutes on iPhone 5 with iOS 6.1.

Using iPhone 4S with same iOS version it taking only 10 seconds, both are using WiFi.

I have tried different cache Policy and timeout Interval of NSURLRequest, it changed nothing, it's still taking long time. Download is over HTTP.

I'm using NSURLConnection class, before downloading this "big" file I'm downloading two others.

Don't know what else can be important,to reduce the time.

Thanks in advance.

Code:

@interface MyClass : NSObject <NSURLConnectionDelegate>
{
  @private id delegate;
  NSURLConnection *connection;
  NSMutableData* responseData;
  //...
}


#import "MyClass.h"

@implementation MyClass

-(void)getObj1:(id)delegateObj
{
   delegate = delegateObj;

   NSString *url = @"...";

   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:120.0];

   connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

   if(connection)
   {
      responseData = [NSMutableData data];
   }
}

-(void)getObj2:(*String)somesString
{

   NSString *url = @"...";

   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:120.0];

   connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

   if(connection)
   {
    responseData = [NSMutableData data];
   }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{ 
    //....
}


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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   if(firstRequest)
   {
      //save data from first request and set responseData clear
      [self getObj2:@"..."];
   }
}

and others without anything special, I hope this will be enough

I have found this post https://devforums.apple.com/message/754875#754875 but still doesn't work fine for me. However now I better understand this strange situation.

like image 950
Meryl Avatar asked Mar 23 '13 18:03

Meryl


1 Answers

Use AFDownloadRequestOperation (AFNetworking "sublass") - you can have also pause/resume operation.

Here you have an example https://stackoverflow.com/a/12209618

like image 64
TonyMkenu Avatar answered Oct 05 '22 22:10

TonyMkenu