NSURLSession是在一个单独的线程中发生的吗?

编程入门 行业动态 更新时间:2024-10-14 18:13:25
本文介绍了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是在一个单独的线程中发生的吗?

本文发布于:2023-10-24 20:24:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是在   线程   发生   NSURLSession

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!