Reputation: 143
We're creating application which sometimes performs synchronization in order to update database state. Sometimes it might be about 100 requests with quite big response performed one after the other. In order to handle full synchronization we have created separate AFURLSessionManager instance. Another one manager was designated for requests not related with the sync process (which must be handled in main thread).
// Manager for requests related with sync process
syncManager = AFURLSessionManager(sessionConfiguration: ...)
syncManager.completionQueue = dispatch_queue_create("sync-queue", DISPATCH_QUEUE_SERIAL)
// Manager for requests not related with sync process (must be handled in main thread)
defaultManager = AFURLSessionManager(sessionConfiguration: ...)
We discovered that for older devices (iPhone 4/4S) executing requests with default manager while synchronization is in progress did consume more time. We've decided to use global queue for sync manager as described below:
syncManager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0)
It improves application performance. My question to you is whether using global queue (instead of serial queue) as completion queue for AFURLSessionManager is safe? Have you ever used simillar approach? I would appreciate any of your help!
Upvotes: 2
Views: 513
Reputation: 5698
The global queues are actually very different from a serial queue you create-- because the global queues are concurrent. Tasks placed on a global queue will automatically be placed in an ideal thread by the OS.
The 'risk' associated with concurrent queues is that because tasks can run at the same time on separate threads, you could end up with issues if you are reading/writing the same properties across separate threads.
As you said you are updating a database, this makes it inherently not safe for you to use a concurrent queue to read/write to the database.
If you are only reading, or only writing, it shouldn't be as much of an issue. But still a risk
My only experience was making hundreds of requests that both read/wrote to a database, and I used a serial queue with no issues
Upvotes: 1