From 744bfc200e8cd6bbb12bc25bc4019ea87a917ff7 Mon Sep 17 00:00:00 2001 From: Wang Zichong Date: Tue, 2 Jun 2026 16:49:07 +0800 Subject: [PATCH] fix: app hang a few seconds when launch app for the first time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复启动器首次启动应用时,可能导致界面卡死几秒的问题. Log: --- src/ddeintegration/appmgr.cpp | 59 ++++++++++++++++++++--------------- src/ddeintegration/appmgr.h | 2 +- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/ddeintegration/appmgr.cpp b/src/ddeintegration/appmgr.cpp index 9a8e9217..5c852328 100644 --- a/src/ddeintegration/appmgr.cpp +++ b/src/ddeintegration/appmgr.cpp @@ -155,37 +155,22 @@ AppMgr::AppMgr(QObject *parent) watchingAppItemRemoved(key); }); - DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this); - if (!config->isValid()) { - qCWarning(logDdeIntegration) << "DConfig is invalid when getting launched times."; - } else { - 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 &value = config->value(AppsLaunchedTimes).toMap(); - updateAppsLaunchedTimes(value); - }); - } - if (isValid()) { + qCInfo(logDdeIntegration) << "AppManager1 service already ready, fetching app items right away"; fetchAppItems(); - } - - m_serviceWatcher = new QDBusServiceWatcher(QStringLiteral("org.desktopspec.ApplicationManager1"), + loadAppsLaunchedTimes(); + } else { + auto serviceWatcher = new QDBusServiceWatcher(QStringLiteral("org.desktopspec.ApplicationManager1"), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration, this); - connect(m_serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this]() { - qCInfo(logDdeIntegration) << "AppManager1 service registered on bus, fetching app items"; - fetchAppItems(); - }); + connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this, serviceWatcher]() { + qCInfo(logDdeIntegration) << "AppManager1 service registered on bus, fetching app items"; + fetchAppItems(); + loadAppsLaunchedTimes(); + serviceWatcher->disconnect(); + }); + } } AppMgr::~AppMgr() @@ -483,6 +468,28 @@ void AppMgr::updateAppsLaunchedTimes(const QVariantMap &appsLaunchedTimes) } } +void AppMgr::loadAppsLaunchedTimes() +{ + DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this); + if (!config->isValid()) { + qCWarning(logDdeIntegration) << "DConfig is invalid when getting launched times."; + } else { + 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 &value = config->value(AppsLaunchedTimes).toMap(); + updateAppsLaunchedTimes(value); + }); + } +} + void AppMgr::fetchAppItems() { qCDebug(logDdeIntegration) << "Begin to fetch apps."; diff --git a/src/ddeintegration/appmgr.h b/src/ddeintegration/appmgr.h index 8d8b0aa0..6fed576e 100644 --- a/src/ddeintegration/appmgr.h +++ b/src/ddeintegration/appmgr.h @@ -64,6 +64,7 @@ private slots: private: void fetchAppItems(); + void loadAppsLaunchedTimes(); void watchingAppItemAdded(const QString &key, AppMgr::AppItem *appItem); void watchingAppItemRemoved(const QString &key); void watchingAppItemPropertyChanged(const QString &key, AppMgr::AppItem *appItem); @@ -72,7 +73,6 @@ private slots: private: __AppManager1ApplicationObjectManager *m_objectManager; - QDBusServiceWatcher *m_serviceWatcher = nullptr; QMap m_appItems; QMap m_pendingAppItems; QTimer *m_checkTimer;