【问题标题】:NSURLSessionDownloadTask file removed before reading contents在读取内容之前删除 NSURLSessionDownloadTask 文件
【发布时间】:2016-11-23 13:30:40
【问题描述】:

我有以下一段代码,它已经运行了几个月,并且刚刚开始出现断断续续的问题。

我正在更改它的调用方式,所以这并不完全令人惊讶,但这些症状让我感到困惑。某些请求正在下载的临时文件似乎在读取数据之前已被删除。

我有关于文件的属性,它的位置,当在模拟器中运行时,我什至确认文件包含正确的数据,而在断点中。但是,一旦调用 [NSData dataWithContentsOfFile:[location path]][NSData dataWithContentsOfURL:location],文件就会从文件系统中删除,如果我使用传入 NSError 的方法,我得到的错误是 Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed.

我的代码:

- (void)makeSoapRequest:(NSURL *)url soapRequest:(NSString *)soapRequest completionBlock:(void (^)(NSData *xmlToParse, int error))completionBlock attemptNumber:(int)attemptNumber
{
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:nil];
    NSData *bodyData = [soapRequest dataUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"" forHTTPHeaderField:@"SOAPAction"];
    [request addValue:[NSString stringWithFormat:@"%d",(int)[bodyData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:bodyData];

    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    if (!(location == nil))
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error)
            {
                NSLog(@"nsurl error was: %@", error);
            }
            else
            {
                NSError* errorObj;
                NSString *path = [location path];

                NSDictionary* attribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&errorObj];
                NSData *xmlData= [NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:&errorObj];
                if (errorObj != nil) {
                    NSLog(@"Attributes of file: %@", attribs);
                    NSLog(@"Error reading the downloaded data, error was: %@", errorObj);
                }
                completionBlock(xmlData, 0);
                return;
            }
        });
    }
    else
    {
        if (error)
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                completionBlock(nil,-1);
            });
        }
        else
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                completionBlock(nil,-1);
            });
        }
    }
}];
[downloadTask resume];
}

我的属性打印输出:

Attributes of file: {
NSFileCreationDate = "2016-11-23 13:15:00 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 501;
NSFileGroupOwnerAccountName = mobile;
NSFileModificationDate = "2016-11-23 13:15:00 +0000";
NSFileOwnerAccountID = 501;
NSFileOwnerAccountName = mobile;
NSFilePosixPermissions = 384;
NSFileReferenceCount = 1;
NSFileSize = 947;
NSFileSystemFileNumber = 534146;
NSFileSystemNumber = 16777218;
NSFileType = NSFileTypeRegular;
}

错误日志:

2016-11-23 13:27:43.285 GCMMobile[2780:829109] Error reading the downloaded data, error was: Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x1704605c0 {NSFilePath=/private/var/mobile/Containers/Data/Application/BF6BE574-AA1A-492E-A1DD-04334F33E2D7/tmp/CFNetworkDownload_VHyKp6.tmp, NSUnderlyingError=0x17024ce10 "The operation couldn’t be completed. No such file or directory"}

最感谢的任何帮助...

【问题讨论】:

    标签: ios nsurlsessiondownloadtask


    【解决方案1】:

    问题是您在移动或打开下载的文件之前从NSURLSessionDownloadTask 完成处理程序返回。正如documentation 所说:

    您必须在完成之前移动此文件或将其打开以供阅读 处理程序返回。否则,文件被删除,数据丢失。

    通过分派到主队列并返回,您将文件读取代码推迟到未来某个不确定的时间。您应该在完成处理程序本身中移动或打开文件,然后才分派到另一个队列进行后续处理。

    【讨论】:

    • 感谢您 - 我将除 completionBlock() 之外的所有内容都移到了 dispatch_async 之外,我的问题就消失了。我想当我开始开发这个以前从未遇到过的问题时,我很幸运。我显然没有通过调用 dispatch_async 来表示我会从我的方法返回,因此该文件将被删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    相关资源
    最近更新 更多