Dispatch_async and [NSURLConnection sendSynchronousRequest]

There are several questions in this thread, and many tips saying that DO NOT use sendSynchronousRequest in dispatch_async because it blocks the thread, and the GCD generates many new workflows to serve all synchronous URL requests.

No one has a definitive answer as to what iOS 5, [NSURLConnection sendAsynchronousRequest: queue: completionHandler:] is doing behind the scenes.

One read message says that it can "optimize" and it can "use the execution loop", but, of course, will not create a new thread for each request.

When I pause my debugger when using sendAsynchronousRequest: queue: completeHandler, the stack trace looks like this:

screenshot

.. it now seems that sendAsynchronousRequest: queue: completHandler actually calls sendSynchronousRequest, and I still have tons of threads created when I use the async method instead of the synchronization method.

Yes, there are other advantages to using an asynchronous call, which I do not want to discuss in this post.

All I'm interested in is using performance / thread / system, and if Iโ€™m worse off using the synchronization call inside dispatch_async instead of using the asynchronous call.

I also don't need advice on using ios4 asynchronous calls, this is purely for educational purposes.

Does anyone have insightful answers to this?

thanks

+6
source share
2 answers

It is truly open source. http://libdispatch.macosforge.org/

It is highly unlikely that you will be able to manage workflows more efficiently than Apple's implementation. In this context, โ€œasynchronousโ€ does not mean select / poll, it just means that the call will return immediately. Therefore, it is not surprising that the implementation spawns threads.

0
source

As mentioned in the previous answer, both GCD (libdispatch) and libblocksruntime are open source . Internally, iOS / OS X manages the global pthreads pool, as well as any application pools that you create in your custom code. Since there is a 1: N mapping between OS threads and sending tasks, you donโ€™t need (and should not) worry about creating and deleting threads under the hood. For this purpose, the GCD task does not use any time in the application launch cycle after the call, since it discards the background thread.

Most types of NSURL operations are related to I / O; there is no amount of concurrency voodo that can mask this, but if Apple's own implementation of asynchronous use uses a synchronous analog in the background, this probably suggests that it is quite optimized. Unlike the previous answer, libdispatch uses built-in scalable I / O ( kqueue on OS X / iOS / BSD), and if you manually did something manually, you would have to deal with the readiness of the file descriptor to give you the same performance yourself.

Although it is possible, a return on time of invested funds is probably marginal. Stick to Apple's implementation and stop worrying about threads!

0
source

Source: https://habr.com/ru/post/920705/


All Articles