6 Replies
      Latest reply: Nov 17, 2016 2:33 AM by eskimo RSS
      aviads Level 1 Level 1 (0 points)

        Hi,

         

        I'm using a NSURLSession with the backgroundSessionConfiguration to send a NSURLSessionUploadTask in the background.

        I noticed that sometimes after waking by the OS in the app delegate's handleEventsForBackgroundURLSession, I get a call on the NSURLSessionDataDelegate for URLSession:dataTask:didReceiveData *directly before* URLSession:task:didCompleteWithError. They seem to be called by two different threads in the same time. Naturally, processing the didComplete before getting the data in didReceive results in bugs. Is this the normal behavior for background tasks and I should support these parallel out of order calls to the delegate for the same task, or is it a bug and just open a rdar?

         

        Thanks

        • Re: Wrong order received by NSURLSession delegate?
          eskimo Apple Staff Apple Staff (7,180 points)

          When you created the session, how do you set up the delegate queue?

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Wrong order received by NSURLSession delegate?
              aviads Level 1 Level 1 (0 points)

              let conf = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("backgroundSession")

              conf.discretionary = true;

              conf.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

              conf.HTTPMaximumConnectionsPerHost = 4

              networkOperationQueue = NSOperationQueue()     // Member

              networkSessionDelegate = CPURLProtocolSessionDelegate()     // Member implementing NSURLSessionDataDelegate and NSURLSessionTaskDelegate interfaces

              let session = NSURLSession(configuration: conf, delegate: self, delegateQueue: networkOperationQueue)

              // And when sending requests we use

              let task = session.uploadTaskWithRequests(request, fromFile:fileURL)

              task.resume()

              The delegate is called simultaneously with the didCompleteWithError and didReceiveData on two different threads.

                • Re: Wrong order received by NSURLSession delegate?
                  eskimo Apple Staff Apple Staff (7,180 points)

                  The problem here relates to how you construct networkOperationQueue.  By default NSOperationQueue will return a concurrent queue, that is, a queue on which multiple operations can run concurrently.  If you want the operations to be serialised, which is the only sane way to deal with NSURLSession callbacks, set the queue’s maxConcurrentOperationCount to 1.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: Wrong order received by NSURLSession delegate?
                      aviads Level 1 Level 1 (0 points)

                      Thanks!

                      • Re: Wrong order received by NSURLSession delegate?
                        jamesmoschou Level 1 Level 1 (0 points)

                        Hi

                         

                        What does the documentation for the delegateQueue parameter mean when it says:

                        "An operation queue for scheduling the delegate calls and completion handlers. The queue need not be a serial queue. If nil, the session creates a serial operation queue for performing all delegate method calls and completion handler calls."

                         

                        It sounds like this is not correct?

                          • Re: Wrong order received by NSURLSession delegate?
                            eskimo Apple Staff Apple Staff (7,180 points)

                            It sounds like this is not correct?

                            I dunno, that sound correct to me.  NSURLSession does not care if you use a serial queue or not; it works fine either way.  If you’re prepared to handle callbacks out of order, you can use a concurrent queue if you want.  That’s not a good idea IMO, but it’s your choice.

                            Regardless, if you feel that the documentation should be clearer on this point, feel free to file a bug describing your position.  Please post your bug number, just for the record.

                            Share and Enjoy

                            Quinn “The Eskimo!”
                            Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                            let myEmail = "eskimo" + "1" + "@apple.com"