作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,当应用程序进入后台时,我尝试将一些数据上传到服务器。这是我正在使用的代码:
self.session = [self backgroundSession];
我的 session 是这样设置的
- (NSURLSession *)backgroundSession
{
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.uploadSession"];
configuration.HTTPMaximumConnectionsPerHost = 1;
session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
});
return session;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self uploadPossibleDrives];
}
// Start uploading the remaing gps log in th right order:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
for (int i = 0; i < arrayOfGPSLogChunks.count; i++)
{
// This will ensure the chunks are sent in the right order
// Add an operation as a block to a queue:
[queue addOperationWithBlock: ^ {
NSData *requestData;
NSMutableURLRequest *request;
if (i == 0)
{
NSLog(@"Initial drive upload %i",ID.integerValue);
requestData = [NSJSONSerialization dataWithJSONObject:historyToUpload options:NSJSONWritingPrettyPrinted error:nil];
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kInitialUploadDriveURL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
}
else
{
NSLog(@"Chunk %i",i);
NSMutableDictionary *chunk = [[NSMutableDictionary alloc] init];
[chunk setObject:driveID forKey:@"driveID"];
[chunk setObject:[arrayOfGPSLogChunks objectAtIndex:i] forKey:@"gpsLog"];
requestData = [NSJSONSerialization dataWithJSONObject:chunk options:NSJSONWritingPrettyPrinted error:nil];
request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kUploadDrivesChunkURL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
}
NSLog(@"_session: %@",self.session);
NSLog(@"request: %@",request);
NSLog(@"requestData: %lu",(unsigned long)requestData.length);
NSLog(@"uploadTask: %@",self.uploadTask);
self.uploadTask = [self.session uploadTaskWithRequest:request fromData:requestData];
[self.uploadTask resume];
if (i == arrayOfGPSLogChunks.count-1)
{
// All gps logs were uploaded so now we save its state as 'uploaded to server':
NSLog(@"Finished sending to server!");
[self setSentToServerForDriveID:ID];
}
}];
}
现在这是我得到的错误:
我真的希望有人能帮助我。我已经调查了我所知道的一切,但似乎无法弄清楚出了什么问题。我也尝试在不使用 block 的情况下上传,但结果是一样的。
如有任何帮助,我们将不胜感激!
最佳答案
对于使用 backgroundSessionConfiguration 创建的 NSURLSession,您必须使用 uploadTaskWithRequest:fromFile:
。
关于ios - 尝试在后台上传时 NSURLSessionUploadTask 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23056949/
我正在开发一个在 VS 社区 2017 中开发并使用 IIS Express 10 的 .net Core MVVC 项目,我遇到了 TempData 无法在我开发的 3 台计算机中的两台上运行的问题
我是一名优秀的程序员,十分优秀!