Skip to content

Commit a069207

Browse files
committed
fix: app hang a few seconds when launch app for the first time
修复启动器首次启动应用时,可能导致界面卡死几秒的问题. Log:
1 parent f4feb31 commit a069207

2 files changed

Lines changed: 39 additions & 30 deletions

File tree

src/ddeintegration/appmgr.cpp

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -155,37 +155,21 @@ AppMgr::AppMgr(QObject *parent)
155155
watchingAppItemRemoved(key);
156156
});
157157

158-
DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this);
159-
if (!config->isValid()) {
160-
qCWarning(logDdeIntegration) << "DConfig is invalid when getting launched times.";
161-
} else {
162-
static const QString AppsLaunchedTimes(u8"appsLaunchedTimes");
163-
const auto &value = config->value(AppsLaunchedTimes).toMap();
164-
updateAppsLaunchedTimes(value);
165-
QObject::connect(config, &DConfig::valueChanged, this, [this, config](const QString &key) {
166-
if (key != AppsLaunchedTimes) {
167-
qCDebug(logDdeIntegration) << "Ignoring non-appsLaunchedTimes key:" << key;
168-
return;
169-
}
170-
171-
qCInfo(logDdeIntegration) << "appsLaunchedTimes of DConfig changed, updating";
172-
const auto &value = config->value(AppsLaunchedTimes).toMap();
173-
updateAppsLaunchedTimes(value);
174-
});
175-
}
176-
177158
if (isValid()) {
159+
qCInfo(logDdeIntegration) << "AppManager1 service already ready, fetching app items right away";
178160
fetchAppItems();
179-
}
180-
181-
m_serviceWatcher = new QDBusServiceWatcher(QStringLiteral("org.desktopspec.ApplicationManager1"),
161+
loadAppsLaunchedTimes();
162+
} else {
163+
auto serviceWatcher = new QDBusServiceWatcher(QStringLiteral("org.desktopspec.ApplicationManager1"),
182164
QDBusConnection::sessionBus(),
183165
QDBusServiceWatcher::WatchForRegistration,
184166
this);
185-
connect(m_serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this]() {
186-
qCInfo(logDdeIntegration) << "AppManager1 service registered on bus, fetching app items";
187-
fetchAppItems();
188-
});
167+
connect(serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this]() {
168+
qCInfo(logDdeIntegration) << "AppManager1 service registered on bus, fetching app items";
169+
fetchAppItems();
170+
loadAppsLaunchedTimes();
171+
});
172+
}
189173
}
190174

191175
AppMgr::~AppMgr()
@@ -466,21 +450,46 @@ void AppMgr::watchingAppItemPropertyChanged(const QString &key, AppMgr::AppItem
466450

467451
void AppMgr::updateAppsLaunchedTimes(const QVariantMap &appsLaunchedTimes)
468452
{
469-
// need to update times for removed and updated.
453+
bool anyChanged = false;
470454
const auto &appItems = m_appItems.values();
471455
for (const auto item : std::as_const(appItems)) {
472456
auto iter = appsLaunchedTimes.find(item->appId);
473457
qint64 times = 0;
474458
if (iter != appsLaunchedTimes.cend())
475459
times = iter->toLongLong();
476460

477-
// including reset and increase times.
478461
if (item->launchedTimes != times) {
479462
qCDebug(logDdeIntegration) << "LaunchedTimesChanged by DConfig, desktopId" << item->id << "from" << item->launchedTimes << "to" << times;
480463
item->launchedTimes = times;
481-
Q_EMIT itemDataChanged(item->id);
464+
anyChanged = true;
482465
}
483466
}
467+
468+
if (anyChanged) {
469+
Q_EMIT changed();
470+
}
471+
}
472+
473+
void AppMgr::loadAppsLaunchedTimes()
474+
{
475+
DConfig *config = DConfig::create("org.deepin.dde.application-manager", "org.deepin.dde.am", "", this);
476+
if (!config->isValid()) {
477+
qCWarning(logDdeIntegration) << "DConfig is invalid when getting launched times.";
478+
} else {
479+
static const QString AppsLaunchedTimes(u8"appsLaunchedTimes");
480+
const auto &value = config->value(AppsLaunchedTimes).toMap();
481+
updateAppsLaunchedTimes(value);
482+
QObject::connect(config, &DConfig::valueChanged, this, [this, config](const QString &key) {
483+
if (key != AppsLaunchedTimes) {
484+
qCDebug(logDdeIntegration) << "Ignoring non-appsLaunchedTimes key:" << key;
485+
return;
486+
}
487+
488+
qCInfo(logDdeIntegration) << "appsLaunchedTimes of DConfig changed, updating";
489+
const auto &value = config->value(AppsLaunchedTimes).toMap();
490+
updateAppsLaunchedTimes(value);
491+
});
492+
}
484493
}
485494

486495
void AppMgr::fetchAppItems()

src/ddeintegration/appmgr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private slots:
6464

6565
private:
6666
void fetchAppItems();
67+
void loadAppsLaunchedTimes();
6768
void watchingAppItemAdded(const QString &key, AppMgr::AppItem *appItem);
6869
void watchingAppItemRemoved(const QString &key);
6970
void watchingAppItemPropertyChanged(const QString &key, AppMgr::AppItem *appItem);
@@ -72,7 +73,6 @@ private slots:
7273

7374
private:
7475
__AppManager1ApplicationObjectManager *m_objectManager;
75-
QDBusServiceWatcher *m_serviceWatcher = nullptr;
7676
QMap<QString, AppMgr::AppItem *> m_appItems;
7777
QMap<QString, AppMgr::AppItem *> m_pendingAppItems;
7878
QTimer *m_checkTimer;

0 commit comments

Comments
 (0)