kongzue / DialogX

💬 DialogX dialog box component library, easy to use, more customizable, more scalable, easy to achieve a variety of dialog boxes. DialogX对话框组件库,更加方便易用,可自定义程度更高,扩展性更强,轻松实现各种对话框、菜单和提示效果,更有Material You、iOS、MIUI等主题扩展可选
Apache License 2.0
2.08k stars 217 forks source link

WaitDialog遇到耗时操作 转圈好像有点卡顿的感觉,但是使用progressDialog好像没卡顿 #145

Closed amosxb closed 2 years ago

kongzue commented 2 years ago

因为其他操作可能引发了主线程的阻塞,优化逻辑即可解决。 DialogX实际上是采用View方式在Activity上做实现的,而不是Window,独立的Window具有独立的Surface,但目前我们虽然也提供了Window模式和DialogFragment模式(实际上也是基于Window)但依然存在未能完全解决的bug,建议还是优化逻辑解决卡顿问题。

amosxb commented 2 years ago

我把耗时操作放在异步线程,好像也是一样。

amosxb commented 2 years ago

Observable.create(ObservableOnSubscribe { emitter -> val groupDeviceArray = deviceArray(group) val selectedDeviceArray = mutableListOf()

        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() {
            }
        })
kongzue commented 2 years ago

肯定是涉及到主线程(UI)的操作了,检查一下。DialogX使用的转圈动画是直接在主线程绘制的,你也可以在源代码中找到它:https://github.com/kongzue/DialogX/blob/master/DialogX/src/main/java/com/kongzue/dialogx/util/views/ProgressView.java

amosxb commented 2 years ago

大佬,我知道原因了,是我发蓝牙广播那里涉及到主线程耗时的操作,我在考虑发蓝牙广播怎么移到其它线程里去。谢谢大佬了。