Swift:为蓝牙中央管理器选择队列

Swift: Choose queue for Bluetooth Central manager(Swift:为蓝牙中央管理器选择队列)

本文介绍了Swift:为蓝牙中央管理器选择队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发将通过 BLE 与智能设备连接并与之通信的应用程序.

I'm working on the app that will connect with a smart device via BLE and communicate with it.

问题是:处理蓝牙事件的最佳做法是在哪个队列中?

我已经阅读了很多教程,在所有教程中我都发现了这一点:

I've read a lot of tutorials and in all of them I found this:

centralManager = CBCentralManager(delegate: self, queue: nil)

他们选择在主队列(queue: nil)中处理蓝牙事件,但我认为这不是一个好习惯.因为可能有很多查询从中央发送到外围设备,而很多答案从外围发送到中央.

They choose to handle bluetooth events in main queue (queue: nil), but I suppose that it's not good practice. Because it could be a lot of queries send to peripheral device from central and a lot of answers send from peripheral to central.

我认为这可能是应用程序运行缓慢的原因,并且可能会对工作效率产生不利影响,对吗?

I assume this might be the reason of the app working slowly and might detrimentally affect the productivity, am I right?

这会淹没 UI 更新队列吗?

Will this flood the UI update queue?

推荐答案

我在我的蓝牙项目中使用了 CBCentralManager 的 dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0) 一段时间,它工作完美.

^ 从头开始​​.我不建议使用全局队列.原因是全局队列是一个并发队列,而您可能想要一个串行队列.创建一个新的 DispatchQueue(label: "CentralManager") 并将其传递给 CBCentralManager.

^ Scratch that. I wouldn't recommend using the global queue. The reason is that the global queue is a concurrent one and you probably want a serial one. Create a new DispatchQueue(label: "CentralManager") and pass it to the CBCentralManager.

所有委托方法都将被传递到您指定的队列.如果你对这些方法做一些非常简单的操作,我想你可以保留主队列.但最好使用后台队列.

All the delegate methods will be delivered to the queue you specify. If you do some very light operations on these methods, I guess you could keep the main queue. But it is better to use a background queue.

这篇关于Swift:为蓝牙中央管理器选择队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Swift:为蓝牙中央管理器选择队列

基础教程推荐