【问题标题】:AFNetWorking Download Session does not update my fileAFNetWorking 下载会话不更新我的文件
【发布时间】:2014-05-06 03:40:06
【问题描述】:

对于 Objective C 来说还是新手,我发现了惊人的 AFNetworking 网络类。

使用 dic 我有我的代码下载我的文件并写入 NSDocumentDirectory

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://myAdress/Menu.plist"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
{
    NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
    return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@", filePath);

    NSDictionary *dicMenuPlist = [NSDictionary dictionaryWithContentsOfFile:[filePath path]];
    NSLog(@"Dic Menu Plist = %@",dicMenuPlist);


}];
[downloadTask resume];

这工作正常,但是当我更改 Menu.plist 文件中的某些内容时,更改不会出现,我必须删除我的应用程序,然后下载已更改的文件。

我不明白为什么我必须这样做。

有人可以帮帮我吗?

【问题讨论】:

  • 下载前删除本地文件
  • 否,但我希望在不删除应用程序的情况下上传文件,用户必须删除应用程序才能获取最后的数据?? ?
  • 不,意思是删除Menu.plist的本地副本而不是应用程序本身
  • a 是的,我明白了,你的意思是我删除文件然后我下载...这并不能解释为什么 AFNetworking 会这样,但谢谢。
  • 它可能正在缓存文件并且不知道它已被更新。它假定您已经拥有该文件,因此它不会再次下载它。 github.com/AFNetworking/AFNetworking/wiki/…

标签: ios nsurlconnection nsurl afnetworking-2


【解决方案1】:

here给出的答案。

删除目标块中的现有文件。

destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

   NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
   NSURL *fileURL = [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];

   NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
   if ([httpResponse statusCode] == 200) {
      // delete existing file (using file URL above)
   }
   return fileURL;

}

【讨论】:

    【解决方案2】:

    默认方法 AFNetworking 3.0 不会刷新您下载的文件。

    如果你想在 Objective-C +iOS9.0 中重写这个文件,你需要这样做:

    - (NSURLSessionDownloadTask *) downloadDocsFromUrl:(NSString *) url withSuccesBlock:(DocModelBlock) docModelBlock withErrorBlock:(ErrorBlock) errorBlock {
    
        NSURL *URL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    
        NSURLSessionDownloadTask *downloadTask = [self.sessionManager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            NSFileManager *fileManager = [NSFileManager defaultManager];
            NSError *error;
    
            NSURL *documentsDirectoryURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&error];
    
            if ([httpResponse statusCode] == 200) {
                NSURL *urlPath = [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    
                if ([fileManager fileExistsAtPath:urlPath.path]) {
                    [fileManager removeItemAtPath:urlPath.path error:&error];
                }
            }
    
            return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
            if (error) {
                errorBlock(error);
            } else {
                docModelBlock(filePath);
            }
        }];
        [downloadTask resume];
    
        return downloadTask;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多