【问题标题】:Crash with NSURLSessionUploadTask when trying to upload in background尝试在后台上传时,NSURLSession Upload Task 崩溃
【发布时间】:2014-08-01 05:23:27
【问题描述】:

在我的应用程序中,当应用程序进入后台时,我尝试将一些数据上传到服务器。 这是我正在使用的代码:

会话

self.session = [self backgroundSession];

这就是我的会话设置方式

- (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];
        }

    }];

}

错误

现在这是我得到的错误:

我希望有人可以帮助我。我已经调查了我所知道的一切,但似乎无法弄清楚出了什么问题。 我也试过不使用块上传,但结果是一样的。

任何帮助将不胜感激!

【问题讨论】:

  • backgroundSessionConfiguration 好像有问题。当我使用NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 时,理论上它可以工作,但显然不是在后台,这是我的目标。

标签: ios xcode nsurlsession nsurlsessionuploadtask


【解决方案1】:

对于使用 backgroundSessionConfiguration 创建的 NSURLSession,您必须使用 uploadTaskWithRequest:fromFile:。

看到这个问题:NSURLSession and stream upload in background

【讨论】:

  • 我很难在任何地方看到这一点。事实上,我似乎看到了相反的情况。 uploadTaskWithRequest:fromData: 返回一个 NSURLSessionUploadTask。 NSURLSessionUploadTask documentation具体说:“和数据任务不同,可以使用上传任务在后台上传内容。”
  • 是的,它的文档记录很差。但是您指向的文档与我所说的并不矛盾。仅仅因为你可以在后台使用它们并不意味着你可以使用任何你想要的构造函数。看到这个问题:stackoverflow.com/questions/19974673/…
  • 啊,好吧,我现在的措辞有些微妙了(顺便说一句,我很高兴接受你说的话;我真的很惊讶没有一些公平的在我们现在可以指出的文档中明确而清晰的陈述。)
  • 是的,这很不幸,由于实施和文档方面的问题,我自己浪费了很多时间。
猜你喜欢
  • 1970-01-01
  • 2017-06-12
  • 2013-11-27
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多