NSURLSession是在一个单独的线程中发生的吗? [英] Does NSURLSession Take place in a separate thread?

查看:258
本文介绍了NSURLSession是在一个单独的线程中发生的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个使用NSURLSession并考虑将其放在与Grand Central Dispatch不同的线程中的应用程序,但如果NSURLSession在后台自动执行此操作,那么我不必使用GCD,对吗?

I was designing an app that uses NSURLSession and thinking about putting it in a different thread with Grand Central Dispatch, but if NSURLSession automatically does that in the background, I wouldn't have to use GCD then, correct?

换句话说,NSURLSession会在后台自动使用Grand Central Dispatch,所以我们不必担心吗?

So in other words, does NSURLSession automatically use Grand Central Dispatch in the background, so we don't have to worry about it?

推荐答案

是的,

NSURLSession在后台线程中工作。下载始终发生在后台线程上。

NSURLSession does its work in a background thread. The download ALWAYS takes place on a background thread.

没有理由包装你的在GCD调用中调用 NSURLSession (或Swift 3 / Swift 4中的 URLSession )的代码。

There is no reason to wrap your code that invokes NSURLSession (or URLSession in Swift 3/Swift 4) in a GCD call.

您可以通过在delegateQueue参数中传递给init方法的队列来控制其完成方法是否在后台线程上执行。如果传入nil,它会创建一个(后台线程)串行操作队列,在其中调用完成方法。如果你传入 NSOperationQueue.mainQueue(),那么你的完成委托方法将在主线程上调用,你不必在 dispatch_async()调用主线程。

You can control whether its completion methods are executed on a background thread or not by the queue you pass in in the delegateQueue parameter to the init method. If you pass in nil, it creates a (background thread) serial operation queue where your completion methods are called. If you pass in NSOperationQueue.mainQueue() then your completion delegate methods will be invoked on the main thread and you won't have to wrap UI calls in dispatch_async() calls to the main thread.

这篇关于NSURLSession是在一个单独的线程中发生的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆