See Apple documentation for Swift (URLSessionDataTask) and Objective C.
Questions tagged [nsurlsessiondatatask]
265 questions
22
votes
1 answer
Does NSURLSession for HTTP data task (NSURLSessionDataTask) runs in background thread or we will have to provide the queue?
I started to use NSURLSession by avoiding NSURLConnection now a days as it's a new and elegant API provided by Apple. Previously, I used to put call NSURLRequest in GCD block to execute it in background. Here is how I used to do in…

NSPratik
- 4,714
- 7
- 51
- 81
15
votes
2 answers
Pattern for retrying URLSession dataTask?
I'm fairly new to iOS/Swift development and I'm working on an app that makes several requests to a REST API. Here's a sample of one of those calls which retrieves "messages":
func getMessages() {
let endpoint = "/api/outgoingMessages"
let…

bmt22033
- 6,880
- 14
- 69
- 98
15
votes
4 answers
Custom NSURLProtocol with NSURLSession
I'm trying to implement this tutorial which implements a custom NSURLProtocol with NSURLConnection.
https://www.raywenderlich.com/76735/using-nsurlprotocol-swift
It works as expected, but now that NSURLConnection is deprecated in iOS9, I'm trying to…

kupilot
- 494
- 1
- 3
- 15
11
votes
4 answers
NSURLSessionDataTask timeout subsequent requests failing
I am creating a NSMutableRequest:
self.req = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
The timeout is set to be 10 seconds because I don't want the user to wait too long to get…

nicky_1525
- 929
- 1
- 10
- 24
10
votes
1 answer
Throw error from dataTask completionHandler
In swift how do I throw an error within a completion handler like this:
let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
do {
//something
…

Wayneio
- 3,466
- 7
- 42
- 73
10
votes
1 answer
NSURLSession HTTP/2 memory leak
This My Test cases, point out that when using NSURLSession with a HTTP/2 connection there is memory problem.
test1: iOS 9. HTTP/2 server
I use NSURLSession to upload 10M file to a HTTP/2 server, if the file uploaded completed everything is ok, But…

rotoava
- 625
- 8
- 18
8
votes
4 answers
NSMutableURLRequest setTimeoutInterval not working in ios 11.0
I am using URL background session for pdf uploading as data stream, using ios version > 9.0. I am setting timeout interval of 300 sec. Its not working at all. After 10 seconds it gets timeout error.
Piece of code is given below
NSTimeInterval…

Prince Kumar Sharma
- 12,591
- 4
- 59
- 90
8
votes
3 answers
NSURLSessionTask. Suspend does not work
This is what Apple's documentation says regarding suspend method of NSURLSessionTask class
A task, while suspended, produces no network traffic and is not subject to timeouts.
Ok. So I'm running the following simple code:
let url =…

Andrey Chernukha
- 21,488
- 17
- 97
- 161
7
votes
2 answers
How to sync serial queue for URLSession tasks?
Using XCode-8.2.1, Swift-3.0.2 and iOS-10.2.1,
I am trying to call two different URLSession.shared.dataTasks (the first is a simple URL-request and the second is a POST-request).
Since my first dataTask delivers a result that is needed in the…

iKK
- 6,394
- 10
- 58
- 131
7
votes
3 answers
Get the size of an AVAssetDownloadTask before downloading
I'm currently implementing offline streaming with FairPlay streaming. Therefor I'm downloading streams using an AVAssetDownloadTask.
I want to give the users feedback about the size of the download which starts to begin:
Are you sure you want to…

Antoine
- 23,526
- 11
- 88
- 94
5
votes
1 answer
How to tell the main thread that a URLSessionDataTask has finished
Using Swift 4, I have this code that attempts a POST request to a REST API:
spinner.startAnimation(self)
btnOk.isEnabled = false
btnCancel.isEnabled = false
attemptPost()
spinner.stopAnimation(self)
btnOk.isEnabled = true
btnCancel.isEnabled =…

David Mordigal
- 813
- 2
- 9
- 22
5
votes
4 answers
Network requests are failing - NSURLSession - App Sandbox - Xcode 9
I have a simple GET request that I am trying to run in my macOS application. However I keep getting the following error:
A server with the specified hostname could not be found.
The URL I am trying to download JSON data from…

Supertecnoboff
- 6,406
- 11
- 57
- 98
5
votes
1 answer
is session.dataTask asynchronous in swift?
I am making requests to my server using the following code:
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration)
let dataTask = session.dataTask(with: request as URLRequest) { (data, response,…

alionthego
- 8,508
- 9
- 52
- 125
5
votes
1 answer
Implementing completion handlers for backgroundSession.uploadTask
I have (almost) successfully implemented URLSessionDelegate, URLSessionTaskDelegate, and URLSessionDataDelegate so that I can upload my objects in the background. But I'm not sure how to implement completion handlers, so that I can delete the object…

Developer
- 736
- 7
- 30
5
votes
1 answer
iOS background task using NSURLSessionDataTask
I have flight search feature in my application which is taking too long to get the data (more than 25 seconds). if application goes background or went to sleep mode, internet connection get disconnected.
I have written below logic using apple…

Jay
- 153
- 2
- 13