gpt4 book ai didi

ios - swift,在同一任务的线程中再次调用网络

转载 作者:行者123 更新时间:2023-11-29 00:36:37 32 4
gpt4 key购买 nike

很抱歉初学者的问题。我有一个 Action 依赖于从网络返回的数据的结果,这个 Action 可能需要另一个网络请求。由于已经在数据任务中调用了第一个网络请求,我想对第二个网络请求使用同一个线程,但不知道该怎么做。任何提示?谢谢

 let task = URLSession.shared.dataTask(with: request  as URLRequest )
{ data, response, error in

if func_1 (data) {
return
}else {
//call another network request here, but don't want to do
//another task = URLSession.shared.... again since we are already on an async thread

}
}

最佳答案

//call another network request here, but don't want to do 
//another task = URLSession.shared.... again since we are already on an async thread

这是误会。此时创建另一个网络请求没有问题。所做的只是将另一个请求放在适当的队列中。安排时在哪个线程上运行并不重要。

我强烈推荐 Apple 的 Concurrency Programming Guide .您可能对 iOS 并发的工作原理有一些误解。 iOS 工作的一个关键部分是了解 GCD 队列,尤其是它们与线程的区别。

关于ios - swift,在同一任务的线程中再次调用网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40450690/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com