【发布时间】: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