以下のコードは、圧縮されたJSONまたは圧縮されたjpgイメージで構成されるデータ "パッケージ"をダウンロードするために使用します。問題は、イメージのzipファイルのダウンロードです。問題のファイルは5 MBで、64 GBのiphone 7でダウンロードするのに約10分かかり、かなり寛大にプロビジョニングされたiMacで動作するシミュレータで約5分かかります。AFNetworkingを使用したダウンロード速度が非常に遅い
受信したデータは保存されますが、すべてのファイルがダウンロードされるまで処理されないため、ダウンロードが完了するまで他の場所で処理する必要はありません。
これは、Webブラウザを使用してファイルを無視できるほどの時間でダウンロードできるため、非常に過剰です。私は様々な質問と答えを見てきましたが、役に立たないものは何も見つかりませんでした。
ご協力いただければ幸いです。 NSURLSessionDownloadTask
は、(例えば、ZIPファイルとして)大量のデータをダウンロードし、ディスク上に格納するために使用されるのに対し、一般に
-(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;
}
あなたを助けないかもしれませんが、まずはあなたの機能を「基本的な」AFNetworkingダウンロードタスクコードに置き換えてください。そのスピードがどのようなものかを見てください。それが速ければ、あなたが物事を遅くしている何かを打つまで、あなたが上記の他の機能に戻って追加してください。 – DonMag
@DonMagありがとう - 私はこの作品を書き直し、何が起こるか見る。オフハンドでは、私はもっと最小限の実装を考えるのは難しいです。 NSURLSessionDownloadTaskは、のような大量のデータを(ダウンロードするために使用されているのに対し、 – jmf1205
うーん...あなたは、このための論理的な理由があるかもしれませんが...一般的には、NSURLSessionDataTaskは、メモリで操作されているデータの小さなビットのために使用されていますzipファイル)を作成し、ディスクに保存します。おそらく、大きなzipにはDownloadTaskを使用し、JSONデータにはDataTaskを使用する必要があります。 – DonMag