2017-04-06 10 views
2

AFNetWorking.Its를 사용하여 파일을 다운로드하는 중입니다. 그리고 "/ Documents"에 파일을 저장했습니다. 그러나이 요청을 다시 호출 할 때 응용 프로그램이 다시 다운로드되었습니다. NSURLCache와 같은 파일을 캐시 할 수있는 솔루션이 있습니까? 다시 요청하고 AF가 폴더에서 발견되면 다시 다운로드하지 않고 읽습니다.AFNetworking의 캐시 파일을 다운로드 중

여기 내 코드가 있습니다.

AFHTTPSessionManager * manager = [AFHTTPSessionManager manager]; 

manager.requestSerializer = [AFHTTPRequestSerializer serializer]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 

NSString * urlString = @"http://wiki.chinaxue.com/images/e/ee/super.docx"; 
NSString * string = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 

NSURLRequest * requesturl = [NSURLRequest requestWithURL:[NSURL URLWithString:string]]; 

NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:requesturl progress:^ (NSProgress * _Nonnull downloadProgress) { 
    // 
    NSLog(@"%lld---%lld",downloadProgress.totalUnitCount, downloadProgress.completedUnitCount); 
} destination:^NSURL *_Nonnull(NSURL *_Nonnull targetPath, NSURLResponse * _Nonnull response) { 

    NSString * DocumentPath = [KYCachedManager Share].documentPath; // filePath where download files stored 

    NSString * documentName = [NSString stringWithFormat:@"%@",response.suggestedFilename]; 
    NSString *path = [DocumentPath stringByAppendingPathComponent:documentName]; 

    MBLog(@"%@",path); 

    return [NSURL fileURLWithPath:path]; 

} completionHandler:^(NSURLResponse *_Nonnull response, NSURL * _Nullable filePath,NSError * _Nullable error) { 
    MBLog(@"%@",filePath); 
    complete(filePath); 
}]; 

downloadTask에서 "response.suggestedFilename"을 가져 오려고 시도했지만 실패했습니다. 다른 질문 : 어떻게 요청없이 response.suggestedFilename을 얻을 수 있습니까? 그런 다음 사용자 fileManager를 사용하여 직접 찾을 수 있습니다. 찾으면 다른 요청을 읽습니다.

답변

1

AFNetworking은 NSURLCache가 이미 제공 한 캐싱 기능을 활용하지만 설정해야합니다.

동료로 캐시를 설정 한 다음 다운로드 할 때 자신의 코드를 사용하십시오.

- (void)setupCache 
{ 
     NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:1024*1024*4 // 1MB mem 
             cachediskCapacity:1024*1024*5 // 5MB disk cache                          
                 diskPath:nil]; 
     [NSURLCache setSharedURLCache:urlCache]; 
} 
    //for lower iphone device like iphone 5 
- (void)setupCache 
{ 
SDURLCache *urlCache = [[SDURLCache alloc] initWithMemoryCapacity:1024*1024 // 1MB mem cache 
                diskCapacity:1024*1024*5 // 5MB disk cache 
                 diskPath:[SDURLCache defaultCachePath]]; 
[NSURLCache setSharedURLCache:urlCache]; 
} 
+1

나는 앱을 단계별로 실행합니다. 먼저 파일 종료를 요청한 다음 Wi-Fi를 닫고 다시 요청하면 작업이 네트워크 오류로 오류가있는 전체 블록을 실행합니다. 캐시 된 것이 작동하지 않는다는 것을 증명할 수 있습니까? –

+0

어떤 것을 예측하기에 충분한 설명이 아닙니다. –

+0

내 코드를 사용해 보셨습니까? –