Failure to return to calling function from NSURLSession delegate without killing the task

syntheticgio

I'm using Swift in Xcode 6.2 (beta) but had the same problem on the 6.1 release version. I'm trying to use NSURLSession and believe I have it set up correctly (see code below). The problem is that I have a delegate setup to deal with a redirect happening through the code. I actually need to capture the cookies prior to the final redirection and I'm doing this through the delegate:

func URLSession(_:NSURLSession, task:NSURLSessionTask, willPerformHTTPRedirection:NSHTTPURLResponse, newRequest:NSURLRequest, completionHandler:(NSURLRequest!) -> Void )

This works and I'm able to execute code successfully and capture the cookies I need. The problem is that I need to add task.cancel() at the end of the function or else it never seems to complete and return to the delegator (parent?) function. Because of this I lose the results from the redirect URL (although in my current project it is inconsequential). The strange thing is that this was working for a while and seemingly stopped. I don't believe I entered any code that changed it, but something had to happen. Below is the relevant code.

NSURLSession Function:

func callURL (a: String, b: String) -> Void {
    // Define the URL
    var url = NSURL(string: "https://mycorrecturl.com");

    // Define the request object (via string)
    var request = NSMutableURLRequest(URL: url!)

    // Use default configuration
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()

    // Create the NSURLSession object with default configuration, and self as a delegate (so calls delegate method)
    let session = NSURLSession(configuration: config, delegate: self, delegateQueue: nil)

    // Change from default GET to POST (needed to call URL properly)
    request.HTTPMethod = "POST"

    // Construct my parameters to send in with the URL
    var params = ["a":a, "b":b] as Dictionary<String, String>

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)

    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        // Do some other stuff after delegate has returned...
    })
    task.resume()
    return
}

The delegate code:

func URLSession(_:NSURLSession, task:NSURLSessionTask, willPerformHTTPRedirection:NSHTTPURLResponse, newRequest:NSURLRequest, completionHandler:(NSURLRequest!) -> Void ) {
    // Check Cookies
    let url = NSURL(string: "https://mycorrecturl.com")
    var all = NSHTTPCookie.cookiesWithResponseHeaderFields(willPerformHTTPRedirection.allHeaderFields, forURL: url!)

    // Get the correct cookie
    for cookie:NSHTTPCookie in all as [NSHTTPCookie] {
        if cookie.name as String == "important_cookie" {
            NSHTTPCookieStorage.sharedHTTPCookieStorage().setCookie(cookie)
        }
    }
    task.cancel()
}

It used to return to the calling function without calling task.cancel(). Is there anything that looks wrong with the code that would cause it to just hang in the delegate function if task.cancel() isn't called?

Edit: What code would I add to fix this.

Rob

If you are not canceling the request, your willPerformHTTPRedirection should call the completionHandler. As the documentation says, this completionHandler parameter is:

A block that your handler should call with either the value of the request parameter, a modified URL request object, or NULL to refuse the redirect and return the body of the redirect response.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Failure to return to calling function from NSURLSession delegate without killing the task

From Dev

Swift: Return JSON string from NSURLSession task

From Dev

Failure return values from Shell function

From Dev

Failure return values from Shell function

From Dev

Calling Java/Scala function from a task

From Dev

Calling a function in the active view's class from the app delegate

From Dev

Calling a function from the return statement of a void function

From Dev

Calling a function from the return statement of a void function

From Dev

Can I Stop a javaexec Failure Killing a Gradle Task?

From Dev

How to return two variables from a python function and access its values without calling it two times?

From Dev

Calling and killing a parent function with onmouseover and onmouseout events

From Dev

Return an NSString from UIViewController without delegate iOS7

From Dev

Calling return of parent function from the completion handler

From Dev

Calling a function from a class without object

From Dev

Swift calling delegate from didSet

From Dev

c++ Assertion failure on return from invariant function

From Dev

C - Return failure from uint8_t function

From Dev

How do I return a failure value from a bash function?

From Dev

C - Return failure from uint8_t function

From Dev

Calling delegate with multiple functions having return values

From Dev

Return delegate function based on type

From Dev

Calling a Func From a Task

From Dev

Exit from REPL without killing background process

From Dev

Exit from REPL without killing background process

From Dev

Read logs from Logcat without killing the tablet

From Dev

Calling recursive function from a function return only first value

From Dev

Calling Python function from Go and getting the function return value

From Dev

debugger does not return from calling function to callee function

From Dev

NSURLSession delegate methods not called

Related Related

  1. 1

    Failure to return to calling function from NSURLSession delegate without killing the task

  2. 2

    Swift: Return JSON string from NSURLSession task

  3. 3

    Failure return values from Shell function

  4. 4

    Failure return values from Shell function

  5. 5

    Calling Java/Scala function from a task

  6. 6

    Calling a function in the active view's class from the app delegate

  7. 7

    Calling a function from the return statement of a void function

  8. 8

    Calling a function from the return statement of a void function

  9. 9

    Can I Stop a javaexec Failure Killing a Gradle Task?

  10. 10

    How to return two variables from a python function and access its values without calling it two times?

  11. 11

    Calling and killing a parent function with onmouseover and onmouseout events

  12. 12

    Return an NSString from UIViewController without delegate iOS7

  13. 13

    Calling return of parent function from the completion handler

  14. 14

    Calling a function from a class without object

  15. 15

    Swift calling delegate from didSet

  16. 16

    c++ Assertion failure on return from invariant function

  17. 17

    C - Return failure from uint8_t function

  18. 18

    How do I return a failure value from a bash function?

  19. 19

    C - Return failure from uint8_t function

  20. 20

    Calling delegate with multiple functions having return values

  21. 21

    Return delegate function based on type

  22. 22

    Calling a Func From a Task

  23. 23

    Exit from REPL without killing background process

  24. 24

    Exit from REPL without killing background process

  25. 25

    Read logs from Logcat without killing the tablet

  26. 26

    Calling recursive function from a function return only first value

  27. 27

    Calling Python function from Go and getting the function return value

  28. 28

    debugger does not return from calling function to callee function

  29. 29

    NSURLSession delegate methods not called

HotTag

Archive