Synchronization inside concurrent NSOperationQueue of NSURLSession.

We have 7 apis calls on our home screen . We want our home screen to get loaded faster .We have used NSURLSession and use delegate approach to receive data.

If maximumConcurrentOperations (of NSOperationQueue of NSURLSession) is not set to 1 then the dictionary (which gets filled with data in DidReceiveData callbacks) will get accessed by multiple concurrent tasks( operations) . Will dictionary usage get synchronized automatically ?

Replies

Will dictionary usage get synchronized automatically ?

With your description, I assume you are using NSMutableDictionary. Under this assumption the answer is NO.

You should better read this article:
Thread Safety Summary

Simply saying, all the mutable objects are thread-unsafe (unless thread-safety is clearly documented).
You may need to implement synchronizing access to the dictionary explicitly by yourself.
What OOPer said plus…

As I wrote in Wrong order received by NSURLSession, the only sensible way to deal with NSURLSession callbacks is to set the queue’s maxConcurrentOperationCount value to 1.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
We just went through the documentation again (https://developer.apple.com/documentation/foundation/nsurlsession/1410330-datataskwithurl ). This link is about CompletionHandler instance method of NSURLSessiondataTask.

It was our mistake that the main statement in documentation got skipped while reading .
That statement is ...

Delegate methods for handling authentication challenges, however, are still called.

We did not want to compromise security and hence we did not use completionHandler approach. But reading above statement it means, even if we use completionHandler approach the necessary callback (DidReceiveChallenge()) does not get skipped. And that is what we wanted.

Conclusion : I don't think we need to set maxConcurrentOperationCount to 1 while using completionHandler approach described in above documentation link. With completionHandler approach the delegate queue can be a real concurrent queue and it can execute many tasks simultaneously.

OOPer and eskimo, please report your opinion on my conclusion specified here.

Once again, thanks a lot.

I don't think we need to set maxConcurrentOperationCount to 1 while
using completion handler approach described in above documentation
link.

While that may work I still recommend against it. A concurrent queue doesn’t buy you anything because the authentication challenge API is already asynchronous.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"