使用backgroundSessionConfiguration和NSURLSessionUploadTask上传会导致应用崩溃 [英] Uploads using backgroundSessionConfiguration and NSURLSessionUploadTask cause app to crash

查看:402
本文介绍了使用backgroundSessionConfiguration和NSURLSessionUploadTask上传会导致应用崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSURLSessionUploadTask进行新的花哨iOS 7后台上传,并且当我使用defaultSessionConfiguration运行时它似乎工作,但是一旦我尝试backgroundSessionConfiguration它就会在我调用uploadTaskWithRequest的行崩溃:

I'm trying out the new fancy iOS 7 background uploading using NSURLSessionUploadTask and it seems to work when I run with defaultSessionConfiguration, but once I try backgroundSessionConfiguration it crashes at the line where I call uploadTaskWithRequest:

以下是代码示例。奇怪的是,虽然在线有无数的downloadTaskWithRequest示例,但我找不到一个将背景和上传结合在一起的单个。

Here is the code sample below. Oddly, while there are myriad downloadTaskWithRequest examples online, I cannot find a single one that combines background and uploading together.

//Create a session w/ background settings
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:@"identifierString.foo"];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

//Create a file to upload
UIImage *image = [UIImage imageNamed:@"onboarding-4@2x.png"];
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSString *documentsDirectory = [[URLs objectAtIndex:0] absoluteString];
NSString *filePath = [documentsDirectory stringByAppendingString:@"testfile.png"];
[imageData writeToFile:filePath atomically:YES];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://file.upload/destination"]];
[request setHTTPMethod:@"PUT"];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:[NSURL URLWithString:filePath] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //code
}];

[uploadTask resume];

此代码在uploadTaskWithRequest的行中崩溃:...就在它到达恢复行之前最后。

This code is crashing at the line with uploadTaskWithRequest: ... just before it gets to the resume line at the end.

奇怪的是,当我使用除backgroundSessionConfiguration之外的任何配置类型时,这似乎工作正常。需要帮助!

Oddly, this seems to work OK when I use any config type other than backgroundSessionConfiguration. Help needed!

提前致谢。

推荐答案

好的,所以这有点像我这里愚蠢而不彻底:

Okay, so this was kind of just me being foolish and not thorough here:

1)我设置了一个异常断点来获取阻止我看到实际异常的堆栈跟踪错误打印输出 - oops。

1) I'd set an exception breakpoint to get stack traces that was preventing me from see the actual exception error printout -- oops.

2)不能使用具有backgroundSessionConfiguration完成回调的uploadTaskWithRequest版本(这并不奇怪但仍未详细记录)。

2) Can't use version of uploadTaskWithRequest that has a completion callback for a backgroundSessionConfiguration (not surprising but still not well documented).

3)将您的PNG数据写入/ var / ...并使用file:/// var / ...将其提供给uploadTaskWithRequest(这只是尴尬因为你不喜欢通常需要在两个命令序列之间进行转换。

3) Write your PNG data to /var/... and provide it to uploadTaskWithRequest with file:///var/... (this is just awkward because you don't often need to convert between the two for a single sequence of commands)

很高兴在这里提供一个NSUrlSessionUploadTask示例代码,因为它们似乎只有零整个互联网。 LMK,如果有人想要的话。

Happy to put up a NSUrlSessionUploadTask sample code here, since there seems to be zero of them on the entire interwebs. LMK if anyone wants that.

这篇关于使用backgroundSessionConfiguration和NSURLSessionUploadTask上传会导致应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆