Swift:为Bluetooth Central Manager选择队列 [英] Swift: Choose queue for Bluetooth Central manager

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

问题描述

我正在开发可通过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)

他们选择处理主队列中的蓝牙事件队列:无),但是我想这不是好的做法。因为可能会有很多查询从中央设备发送到外围设备,并且有很多答案从外围设备发送到中心设备。

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:为Bluetooth Central Manager选择队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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