3

「Last-Modified」ヘッダーを含むファイルをダウンロードしようとしています。ファイルが変更されていない場合は、必要に応じて 304 を取得し、ファイルの再ダウンロードを保存します。ただし、

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

304 を取得すると、ファイルが空のドキュメントで上書きされます。

ここに私のコードの残りの部分があります:

- (void) addPDFToDownloadQueue:(NSString *)pdf{
    if (![pdf isEqualToString:@""]) {
        NSString *urlString = [API_BASE_URL_PDF stringByAppendingPathComponent:pdf];
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                               cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                           timeoutInterval:60];
        if ([UserDefaultManager getEtagForKey:pdf]) {
            NSString *etag = [UserDefaultManager getEtagForKey:pdf];
            [request setValue:etag forHTTPHeaderField:IF_MODIFIED_SINCE];
        }
        [request setHTTPMethod: API_METHOD_GET];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        NSString *path = [FileUtils getPathInDocumentsWithFileName:pdf] ;
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([operation.response respondsToSelector:@selector(allHeaderFields)]) {
                [UserDefaultManager setEtag:[operation.response.allHeaderFields objectForKey:LAST_MODIFIED] forKey:pdf];
            }
            if ([self checkIfQueueHasFinished]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_DOWNLOAD_ALL_PDF_FINISHED object:nil];
            };


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (operation.response.statusCode != RESPONSE_CODE_UP_TO_DATE) {
                LogDebug(@"%@", [error localizedDescription]);
            }
            if ([self checkIfQueueHasFinished]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_DOWNLOAD_ALL_PDF_FINISHED object:nil];
            };
        }];


        [self.downloadQueue addOperation:operation];
    }
4

0 に答える 0