Closed amosxb closed 2 years ago
我把耗时操作放在异步线程,好像也是一样。
Observable.create(ObservableOnSubscribe
val deviceArray = findAllDevice()
for (device in deviceArray) {
if (ids.contains(device.id)) {
selectedDeviceArray.add(device)
}
}
val removeArray =
groupDeviceArray.filterNot { selectedDeviceArray.contains(it) }.toMutableList()
val addArray =
selectedDeviceArray.filterNot { groupDeviceArray.contains(it) }.toMutableList()
for (device in removeArray) {
removeDevice(group, device)
}
for (device in addArray) {
addDevice(group, device)
}
if (isBroadcast) {
BulbBroadcast.setGroupForDeviceArray(
addArray,
HexUtil.hexStringToBytes(group.gid)
) {
BulbBroadcast.removeGroupForDeviceArray(
removeArray,
HexUtil.hexStringToBytes(group.gid)
) {
emitter.onNext(it)
emitter.onComplete()
}
}
}
}).observeOn(AndroidSchedulers.mainThread()) //观察者所在线程,即后台任务执行完成后需要回调的线程
.subscribeOn(Schedulers.io()) //被观察者所在的线程,在后台任务执行的线程
.subscribe(object : Observer<Boolean> {
override fun onNext(t: Boolean) {
DialogUtils.dismiss()
if (t) {
sendDatabaseHasChanged(this@AddGroupActivity)
clearMultiFinish(this@AddGroupActivity)
} else {
DialogUtils.showFailed()
}
}
override fun onSubscribe(d: Disposable?) {
}
override fun onError(e: Throwable?) {
}
override fun onComplete() {
}
})
肯定是涉及到主线程(UI)的操作了,检查一下。DialogX使用的转圈动画是直接在主线程绘制的,你也可以在源代码中找到它:https://github.com/kongzue/DialogX/blob/master/DialogX/src/main/java/com/kongzue/dialogx/util/views/ProgressView.java
大佬,我知道原因了,是我发蓝牙广播那里涉及到主线程耗时的操作,我在考虑发蓝牙广播怎么移到其它线程里去。谢谢大佬了。
因为其他操作可能引发了主线程的阻塞,优化逻辑即可解决。 DialogX实际上是采用View方式在Activity上做实现的,而不是Window,独立的Window具有独立的Surface,但目前我们虽然也提供了Window模式和DialogFragment模式(实际上也是基于Window)但依然存在未能完全解决的bug,建议还是优化逻辑解决卡顿问题。