gpt4 book ai didi

ios - NSURLSessionDownloadTask 文件在读取内容之前被移除

转载 作者:行者123 更新时间:2023-11-28 21:18:21 24 4
gpt4 key购买 nike

我有以下一段代码,它已经工作了几个月,并且刚刚开始间歇性地损坏。

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

我有关于文件的属性,它的位置,当在模拟器中运行时,我什至确认文件包含正确的数据,同时在断点中。但是,一旦调用 [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"}

非常感谢任何帮助...

最佳答案

问题是您在移动或打开下载的文件之前从 NSURLSessionDownloadTask 完成处理程序返回。作为documentation状态:

You must move this file or open it for reading before your completion handler returns. Otherwise, the file is deleted, and the data is lost.

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

关于ios - NSURLSessionDownloadTask 文件在读取内容之前被移除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40765881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com