我有一个应用程序上传TSPhoto对象数组中保存的几张图片。NSURLSessionUploadTask“完成”,但没有网络流量
当我开始上传,委托方法最终触发说,它的完成,但应用始终命中服务器和工具没有显示任何网络流量:
_session = [self backgroundSession];
for(TSPhoto *photo in _picsetPhotos) {
NSURL *uploadURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@upload", @"https://example.com/"]];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:uploadURL];
[req setHTTPMethod:@"POST"];
NSLog(@"req dump: %@", req);
// Add it to the queue
NSURLSessionUploadTask *uploadTask = [_session uploadTaskWithRequest:req fromFile:[NSURL fileURLWithPath:photo.fullImagePath]];
// "Start" it
NSLog(@"Enqueueing %@", uploadTask);
[uploadTask resume];
NSLog(@"Post-resume");
}
-backgroundSession:
-(NSURLSession *)backgroundSession {
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"TSBackgroundUploader"];
configuration.HTTPMaximumConnectionsPerHost = 1;
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return session;
}
我已经实现了所有的适当的委托方法:
-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
NSLog(@"Finished all uploads!");
[[[UIAlertView alloc] initWithTitle:@"Upload Completed" message:@"Your album has been created." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
dispatch_async(dispatch_get_main_queue(), ^{
[_uploadProgress setProgress:
(double)totalBytesSent/
(double)totalBytesExpectedToSend animated:YES];
});
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[_uploadProgress setProgress:0.5];
// Did we succeed?
if(!error) {
NSLog(@"Upload done");
} else {
NSLog(@"%@", error.description);
}
});
进度条也起到真的靠不住的过程中这一切,上升和下降,直至最终达到100
任何线索可能什么呢?
编辑: 以下是我添加了一些NSLog
报表后的日志。这些任务以与它们开始相反的顺序呼叫代表:
2015-05-04 09:55:42.492 TS[348:95052] Starting background upload
2015-05-04 09:55:42.493 TS[348:95052] req dump: <NSMutableURLRequest: 0x1740138b0> { URL: https://example.com/upload }
2015-05-04 09:55:42.512 TS[348:95052] Enqueueing <__NSCFBackgroundUploadTask: 0x155d804c0>{ taskIdentifier: 3 }
2015-05-04 09:55:42.513 TS[348:95052] Post-resume
2015-05-04 09:55:42.513 TS[348:95052] req dump: <NSMutableURLRequest: 0x17400de00> { URL: https://example.com/upload }
2015-05-04 09:55:42.524 TS[348:95052] Enqueueing <__NSCFBackgroundUploadTask: 0x155e68db0>{ taskIdentifier: 4 }
2015-05-04 09:55:42.525 TS[348:95052] Post-resume
2015-05-04 09:56:01.726 TS[348:95052] ID: 4
2015-05-04 09:56:01.726 TS[348:95052] Upload done
2015-05-04 09:56:02.920 TS[348:95052] ID: 3
2015-05-04 09:56:02.920 TS[348:95052] Upload done
究竟日志包含什么,以及哪些任务调用委托回来? – Wain
@Wain:编辑添加日志。 – Timothy
你如何测试服务器端的数据?你使用VPN吗?你试过跟查尔斯追踪过吗?当您将每个任务更新为100%时,进度非常混乱,然后当每个任务完成时重置为50%。 – Wain