nsurlsessiontask

How to wait for all tasks of an NSURLSession to complete?

醉酒当歌 提交于 2020-06-16 07:48:37
问题 Why is NSURLSession operation queue empty after creating and resuming an NSURLSessionTask? Is there a way to tell if an NSURLSession has tasks pending? The goal is to wait for multiple tasks to complete, but this doesn't work: NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithStreamedRequest:request]; [uploadTask resume]; // this prints "0" NSLog(self.session.delegateQueue.operationCount) // this returns immediately instead of waiting for task to complete [self.session

How to get data to return from NSURLSessionDataTask in Swift

久未见 提交于 2020-01-14 04:08:14
问题 I have this same question as was asked and answered here: How to get data to return from NSURLSessionDataTask The difference: How can I do this in Swift? I do not know Objective C at all so trying to interpret that answer is proving a bit futile for me.. So given the code below I have a FirstViewController which will call my HomeModel class to go and get the data using a NSURLSession call. Once the call is complete I want return the data to the FirstViewController so that I may go and set it

What is the recommended way to intercept and prevent redirects with AFNetworking 2.0?

戏子无情 提交于 2020-01-02 10:08:22
问题 It seems to me that the proper place to do this is in AFURLSessionManager , in setting the taskWillPerformHTTPRedirection block, but I am unsure of the best way to handle it. Currently, in my AFHTTPSessionManager subclass, I am setting the redirect block globally for all requests, and I know I can prevent redirects by returning nil here: - (void)setupRedirectBlock { [self setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest *(NSURLSession *session, NSURLSessionTask *task, NSURLResponse

How to determine when NSURLSessionTask's request begins?

纵饮孤独 提交于 2019-12-11 03:24:17
问题 I use NSURLSessionTask s and I'm trying to monitor how long some of my HTTP Requests take, what delegate method (or something else) can I monitor for when the NSURLSessionTask actually makes the initial request? If this were a NSURLConnection inside an NSOperation I'd just start a timer when I start the request but I don't have control over when tasks start. 回答1: Please check NSURLSessionTaskDelegate . It has following delegate callbacks: URLSession:task:didCompleteWithError: URLSession:task

What is the recommended way to intercept and prevent redirects with AFNetworking 2.0?

纵然是瞬间 提交于 2019-12-06 08:02:51
It seems to me that the proper place to do this is in AFURLSessionManager , in setting the taskWillPerformHTTPRedirection block, but I am unsure of the best way to handle it. Currently, in my AFHTTPSessionManager subclass, I am setting the redirect block globally for all requests, and I know I can prevent redirects by returning nil here: - (void)setupRedirectBlock { [self setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest *(NSURLSession *session, NSURLSessionTask *task, NSURLResponse *response, NSURLRequest *request) { return nil; }]; } ...but I need to only do this on specific tasks, and

What is difference between NSURLSessionDataTask vs NSURLSessionDownloadTask

本小妞迷上赌 提交于 2019-12-03 10:48:00
问题 In latest apple introduce new NSURLSession in replace of NSURLConnection , so in there are different task , so what is the difference between NSURLSessionDataTask , NSURLSessionDownloadTask ? and in which scenario use NSURLSessionTask and where NSURLSessionDownloadTask ? 回答1: NSURLSessionDataTask : Data tasks exchange data using NSData. NSURLSessionDataTask is not supported in Background Sessions. Data tasks send and receive data using NSData objects. Data tasks are intended for short, often

What is difference between NSURLSessionDataTask vs NSURLSessionDownloadTask

断了今生、忘了曾经 提交于 2019-12-03 01:21:53
In latest apple introduce new NSURLSession in replace of NSURLConnection , so in there are different task , so what is the difference between NSURLSessionDataTask , NSURLSessionDownloadTask ? and in which scenario use NSURLSessionTask and where NSURLSessionDownloadTask ? NSURLSessionDataTask : Data tasks exchange data using NSData. NSURLSessionDataTask is not supported in Background Sessions. Data tasks send and receive data using NSData objects. Data tasks are intended for short, often interactive requests from your app to a server. Data tasks can return data to your app one piece at a time

NSURLSession didCompleteWithError:gets called with NSError is nil

痴心易碎 提交于 2019-12-01 08:50:20
Scenario is taking the app to background and foreground multiple times when its uploading is in progress,didCompleteWithError: method is called while taking app to foreground with error parameter is null. Probabilty is 1/3. How do i find out what went wrong as error parameter does not give anything. The problem is that didCompleteWithError report only client side error, otherwise is nil. iOS doc said: "Server errors are not reported through the error parameter. The only errors your delegate receives through the error parameter are client-side errors , such as being unable to resolve the

NSURLSession didCompleteWithError:gets called with NSError is nil

*爱你&永不变心* 提交于 2019-12-01 06:46:05
问题 Scenario is taking the app to background and foreground multiple times when its uploading is in progress,didCompleteWithError: method is called while taking app to foreground with error parameter is null. Probabilty is 1/3. How do i find out what went wrong as error parameter does not give anything. 回答1: The problem is that didCompleteWithError report only client side error, otherwise is nil. iOS doc said: "Server errors are not reported through the error parameter. The only errors your

How do I unit test HTTP request and response using NSURLSession in iOS 7.1?

£可爱£侵袭症+ 提交于 2019-11-28 17:54:39
I'd like to know how to "unit test" HTTP requests and responses using NSURLSession. Right now, my completion block code does not get called when running as a unit test. However, when the same code is executed from within the AppDelegate (didFinishWithLaunchingOptions), the code inside the completion block is called. As suggested in this thread, NSURLSessionDataTask dataTaskWithURL completion handler not getting called , the use of semaphores and/or dispatch_group is needed "to make sure the main thread is blocked until the network request finishes." My HTTP post code looks like the following.