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

查看:30
本文介绍了使用 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: ...在最后到达resume行之前崩溃了.

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) 我设置了一个异常断点来获取阻止我看到实际异常错误打印输出的堆栈跟踪——哎呀.

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天全站免登陆