The bit of code below is used to download data "packages" consisting of zipped JSON, or zipped jpg images. The issue is the image zip file download. The file in question is 5 MB, and takes about 10 min to download on a 64GB iphone 7, and about 5 min on the simulator running on a fairly generously provisioned iMac.
The received data is stored, but not processed until all files are downloaded, so there should be nothing going on elsewhere until downloading is complete.
This seems quite excessive as I can download the file using a web browser in a negligible period of time. I have looked at various questions and answers and haven't found anything useful.
Any assistance will be much appreciated.
-(NSInteger)getPackageData:(NSString *)url type:(NSInteger)isZip fileName:(NSString *)fileName
item:(NSString *)item
{
__block NSInteger errorCode=0;
isReady=0;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
if(isZip==0){
manager.responseSerializer=[AFJSONResponseSerializer serializer];
}else{
manager.responseSerializer=[AFHTTPResponseSerializer serializer];
}
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
[policy setValidatesDomainName:YES];
manager.securityPolicy = policy;
/****************
for self signed certs
manager.securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy.validatesDomainName = NO;
***************/
NSURL *mURL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:mURL];
[request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:100];
NSURLSessionDataTask *dataTask =
[manager dataTaskWithRequest:request
completionHandler:^(NSURLResponse *response,
id json,
NSError *error) {
if (error) {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
//error condition 1
NSInteger statusCode = [(NSHTTPURLResponse *) response statusCode];
if(statusCode==403){ // unauthorized
errorCode=-1;
}
}
}
else if(isZip==1){
// process Zipped json Files for data update
NSString *filePath = [jsonPath stringByAppendingPathComponent:fileName];
[json writeToFile:filePath options:NSDataWritingAtomic error:&error];
[[NSNotificationCenter defaultCenter] postNotificationName:@"dataReady" object:item];
errorCode=0;
}else if(isZip==2){ //zipped photos file
NSString *filePath = [photoPath stringByAppendingPathComponent:fileName];
CS_LOG(@"Saving URL %@ to photo file %@",url,filePath);
[json writeToFile:filePath options:NSDataWritingAtomic error:&error];
CLS_LOG(@"Saved");
[[NSNotificationCenter defaultCenter] postNotificationName:@"dataReady" object:@"PHOTOSREADY"];
errorCode=0;
}
} ];
[dataTask resume];
return errorCode;
}
In general, NSURLSessionDataTask
is used for small bits of data which are then manipulated in memory, whereas NSURLSessionDownloadTask
is used for downloading large amounts of data (such as a zip file) and storing it on disk.
Perhaps you should use NSURLSessionDownloadTask
for your big zips, and NSURLSessionDataTask
for your JSON data.