fix: app hang a few seconds when launch app for the first time#769
fix: app hang a few seconds when launch app for the first time#769BLumia wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: BLumia The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
修复启动器首次启动应用时,可能导致界面卡死几秒的问题. Log:
deepin pr auto review你好!我是CodeGeeX。我仔细审查了你提供的 整体上,代码的逻辑变得更加清晰,职责划分更明确。但也引入了一些关于内存管理、对象生命周期和代码健壮性的问题。以下是我的详细审查意见: 1. 代码逻辑与内存管理问题: } else {
auto serviceWatcher = new QDBusServiceWatcher(..., this);
connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this]() {
fetchAppItems();
loadAppsLaunchedTimes();
});
}
auto serviceWatcher = new QDBusServiceWatcher(QStringLiteral("org.desktopspec.ApplicationManager1"),
QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForRegistration,
this);
connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this, serviceWatcher]() {
qCInfo(logDdeIntegration) << "AppManager1 service registered on bus, fetching app items";
fetchAppItems();
loadAppsLaunchedTimes();
serviceWatcher->deleteLater(); // 触发后即销毁,避免重复监听和内存浪费
});2. 代码性能问题: QObject::connect(config, &DConfig::valueChanged, this, [this, config](const QString &key) { ... });如果在极端情况下(例如 DBus 服务掉线又重连,虽然当前代码未处理掉线情况),或者由于逻辑变更导致
3. 代码安全与健壮性问题: DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this);
if (!config->isValid()) {
DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this);
if (!config) {
qCWarning(logDdeIntegration) << "Failed to create DConfig instance (nullptr).";
return;
}
if (!config->isValid()) {
qCWarning(logDdeIntegration) << "DConfig is invalid when getting launched times.";
// 注意:如果 config 是 new 出来的且 parent 是 this,虽然最终会被析构,但尽早 delete 更好
config->deleteLater();
return;
}4. 语法与代码风格问题:Lambda 表达式中的变量遮蔽 QObject::connect(config, &DConfig::valueChanged, this, [this, config](const QString &key) {
// ...
const auto &value = config->value(AppsLaunchedTimes).toMap(); // 这里的 value 遮蔽了外层作用域的 value
updateAppsLaunchedTimes(value);
});外层作用域有
综合改进后的代码建议结合以上分析,我为你重构了 appmgr.cpp 构造函数相关部分: if (isValid()) {
qCInfo(logDdeIntegration) << "AppManager1 service already ready, fetching app items right away";
fetchAppItems();
loadAppsLaunchedTimes();
} else {
auto serviceWatcher = new QDBusServiceWatcher(QStringLiteral("org.desktopspec.ApplicationManager1"),
QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForRegistration,
this);
connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this, serviceWatcher]() {
qCInfo(logDdeIntegration) << "AppManager1 service registered on bus, fetching app items";
fetchAppItems();
loadAppsLaunchedTimes();
serviceWatcher->deleteLater(); // 触发一次后不再需要监听
});
}appmgr.cpp void AppMgr::loadAppsLaunchedTimes()
{
DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this);
if (!config) {
qCWarning(logDdeIntegration) << "Failed to create DConfig instance (nullptr).";
return;
}
if (!config->isValid()) {
qCWarning(logDdeIntegration) << "DConfig is invalid when getting launched times.";
config->deleteLater(); // 及时清理无效对象
return;
}
static const QString AppsLaunchedTimes(u8"appsLaunchedTimes");
const auto &value = config->value(AppsLaunchedTimes).toMap();
updateAppsLaunchedTimes(value);
QObject::connect(config, &DConfig::valueChanged, this, [this, config](const QString &key) {
if (key != AppsLaunchedTimes) {
qCDebug(logDdeIntegration) << "Ignoring non-appsLaunchedTimes key:" << key;
return;
}
qCInfo(logDdeIntegration) << "appsLaunchedTimes of DConfig changed, updating";
// 避免变量名遮蔽
const auto &newValue = config->value(AppsLaunchedTimes).toMap();
updateAppsLaunchedTimes(newValue);
});
}进一步优化建议(可选): |
修复启动器首次启动应用时,可能导致界面卡死几秒的问题.