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

2024-04-14移动开发问题
0

本文介绍了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:为蓝牙中央管理器选择队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13