是否可以创建多个DispatchQueue? [英] Is it possible to create multiple DispatchQueue?

查看:117
本文介绍了是否可以创建多个DispatchQueue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对DispatchQueue的东西(包括DispatchGroups,Qos等)不太老.我现在有一个顾虑. 就像,我有一个ViewController,可用于Android的ViewPager UI.在那个VC中,我有一个TableView,每次ViewPager中的页面"被更改时,它都会重新加载. 我在TableView单元格中有一个favourite button属性.当用户点击该按钮时,它将存储所需的信息以进行邮寄( JSON ). 每当viewWillDisappear运行时,我都希望进行发布.在这里,我想为每个页面"创建后台线程,并捕获已经创建的线程,以防用户点击某个页面"而导致后期调用由于任何原因而无法达到顶点. 这是我班上的东西:

I'm not very much old with DispatchQueue things (including DispatchGroups, Qos etc.). And I have a concern right now. It's like, I have a ViewController that is reused for Android's ViewPager UI. And in that VC, I have a TableView which reloads every time the "page" in the ViewPager is changed. I have a favourite button property in the TableView cell. When user tap on that button, it stores required information for a post call (JSON). I want to make the post call whenever viewWillDisappear runs. And there I want to create background threads for each "page" and catch the already created thread in case a user hits a "page" for which the post call could not culminate for any reasons. Here is what I have in my class:

var workItem1: DispatchWorkItem!
var dispatch1: DispatchQueue!

override func viewWillDisappear(_ animated: Bool) {
     super.viewWillDisappear(animated)
}

我应该怎么做才能实现自己的目标?

What should I do to achieve my goal?

推荐答案

理论上,您可以根据需要创建任意数量的队列.您还可以在串行队列(任务相互处理)或并发队列(任务同时处理)之间进行选择

You can theoretically create as many queues as you need. You can also choose between a serial queue (tasks are being worked on after each other) or a concurrent queue (tasks are being worked on concurrently)

let concurrentQueue = DispatchQueue(label: "my_queue_1", attributes: .concurrent)
concurrentQueue.sync {
    //do your thing
}  

BUT::为每个呼叫创建队列是一种不好的做法.那不是队列的目的.相反,您应该使用并发队列,其中每个任务都代表一个POST调用.

BUT: It's bad practice to create a queue for each call. Thats not what queues are for. What you should do instead is use a concurrent queue where every task represents a POST-call.

甚至更好的是,您应该问自己:是否不能一次性将用户喜欢的所有页面的数组提交到服务器.这样可以减少排队的需要.

Or even better you should ask yourself if you can't just submit an array of all the pages the user has favourited to the server in one go. This would eliminate the need for a queue somewhat.

这篇关于是否可以创建多个DispatchQueue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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