Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Completion handler blocks are not supported in background sessions. Use a delegate instead

Tags:

ios

I use background sessions for network activities with following code. However, I encountered an error with crash log below.

Completion handler blocks are not supported in background sessions. Use a delegate instead.

Please provide pointers to resolve this issue.

    NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];
    config.sessionSendsLaunchEvents = YES;
    config.allowsCellularAccess = YES;
    config.discretionary = YES;
    NSURLSession* session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
    NSURLSessionDownloadTask* downloadTask = [session downloadTaskWithRequest:request];
    [downloadTask resume];

Crash log

0   CoreFoundation __exceptionPreprocess + 120
1   libobjc.A.dylib objc_exception_throw + 52
2   CFNetwork -[__NSURLBackgroundSession validateSerializabilityForRequest:completion:] + 196
3   CFNetwork -[__NSURLBackgroundSession _onqueue_downloadTaskForRequest:resumeData:completion:] + 48
4   CFNetwork __90-[__NSURLBackgroundSession downloadTaskForRequest:downloadFilePath:resumeData:completion:]_block_invoke + 32
5   CFNetwork __68-[__NSURLBackgroundSession performBlockOnQueueAndRethrowExceptions:]_block_invoke + 72
6   libdispatch.dylib _dispatch_client_callout + 12
7   libdispatch.dylib _dispatch_barrier_sync_f_invoke + 80
8   CFNetwork -[__NSURLBackgroundSession performBlockOnQueueAndRethrowExceptions:] + 148
9   CFNetwork -[__NSURLBackgroundSession downloadTaskForRequest:downloadFilePath:resumeData:completion:] + 188
!   10 -[CRNSURLSessionTaskProxy initDownloadTaskWithSession:request:completionHandler:dispatch:] + 152
11 -[CRNSURLSessionProxy downloadTaskWithRequest:completionHandler:] + 108
12 -[CRNSURLSessionProxy downloadTaskWithRequest:] + 20
like image 981
GJain Avatar asked Dec 16 '16 20:12

GJain


1 Answers

You need to add a delegate in your downloadTask then implement delegate methods which are described here: link

also good tutorial here: link

like image 115
Vadim Kozak Avatar answered Nov 11 '22 06:11

Vadim Kozak