Remove cache updates from the main thread

This commit is contained in:
Konstantinos Sideris 2017-10-21 21:17:01 +03:00
parent 3cae6c3983
commit 160fe1d668
4 changed files with 27 additions and 26 deletions

View file

@ -41,6 +41,7 @@ endif()
find_package(Qt5Widgets REQUIRED) find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED) find_package(Qt5Network REQUIRED)
find_package(Qt5LinguistTools REQUIRED) find_package(Qt5LinguistTools REQUIRED)
find_package(Qt5Concurrent REQUIRED)
if (APPLE) if (APPLE)
find_package(Qt5MacExtras REQUIRED) find_package(Qt5MacExtras REQUIRED)
@ -318,9 +319,9 @@ if (BUILD_TESTS)
endif() endif()
if(APPVEYOR_BUILD) if(APPVEYOR_BUILD)
set (NHEKO_LIBS matrix_events Qt5::Widgets Qt5::Network lmdb) set (NHEKO_LIBS matrix_events Qt5::Widgets Qt5::Network Qt5::Concurrent lmdb)
else() else()
set (NHEKO_LIBS matrix_events Qt5::Widgets Qt5::Network ${LMDB_LIBRARY}) set (NHEKO_LIBS matrix_events Qt5::Widgets Qt5::Network Qt5::Concurrent ${LMDB_LIBRARY})
endif() endif()
set (NHEKO_DEPS ${OS_BUNDLE} ${SRC_FILES} ${UI_HEADERS} ${MOC_HEADERS} ${QRC} ${LANG_QRC} ${QM_SRC}) set (NHEKO_DEPS ${OS_BUNDLE} ${SRC_FILES} ${UI_HEADERS} ${MOC_HEADERS} ${QRC} ${LANG_QRC} ${QM_SRC})

View file

@ -102,6 +102,7 @@ Cache::setState(const QString &nextBatchToken, const QMap<QString, RoomState> &s
if (!isMounted_) if (!isMounted_)
return; return;
try {
auto txn = lmdb::txn::begin(env_); auto txn = lmdb::txn::begin(env_);
setNextBatchToken(txn, nextBatchToken); setNextBatchToken(txn, nextBatchToken);
@ -110,6 +111,12 @@ Cache::setState(const QString &nextBatchToken, const QMap<QString, RoomState> &s
insertRoomState(txn, it.key(), it.value()); insertRoomState(txn, it.key(), it.value());
txn.commit(); txn.commit();
} catch (const lmdb::error &e) {
qCritical() << "The cache couldn't be updated: " << e.what();
unmount();
deleteData();
}
} }
void void

View file

@ -18,6 +18,7 @@
#include <QApplication> #include <QApplication>
#include <QDebug> #include <QDebug>
#include <QSettings> #include <QSettings>
#include <QtConcurrent>
#include "AvatarProvider.h" #include "AvatarProvider.h"
#include "ChatPage.h" #include "ChatPage.h"
@ -196,6 +197,11 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
this, this,
SLOT(initialSyncCompleted(const SyncResponse &))); SLOT(initialSyncCompleted(const SyncResponse &)));
connect(client_.data(), &MatrixClient::initialSyncFailed, this, [=](const QString &msg) { connect(client_.data(), &MatrixClient::initialSyncFailed, this, [=](const QString &msg) {
if (client_->getHomeServer().isEmpty()) {
deleteConfigs();
return;
}
initialSyncFailures += 1; initialSyncFailures += 1;
if (initialSyncFailures >= MAX_INITIAL_SYNC_FAILURES) { if (initialSyncFailures >= MAX_INITIAL_SYNC_FAILURES) {
@ -426,14 +432,7 @@ ChatPage::syncCompleted(const SyncResponse &response)
} }
} }
try { QtConcurrent::run(cache_.data(), &Cache::setState, response.nextBatch(), state_manager_);
cache_->setState(response.nextBatch(), state_manager_);
} catch (const lmdb::error &e) {
qCritical() << "The cache couldn't be updated: " << e.what();
// TODO: Notify the user.
cache_->unmount();
cache_->deleteData();
}
client_->setNextBatchToken(response.nextBatch()); client_->setNextBatchToken(response.nextBatch());
@ -479,16 +478,10 @@ ChatPage::initialSyncCompleted(const SyncResponse &response)
QApplication::processEvents(); QApplication::processEvents();
} }
try {
cache_->setState(response.nextBatch(), state_manager_);
} catch (const lmdb::error &e) {
qCritical() << "The cache couldn't be initialized: " << e.what();
cache_->unmount();
cache_->deleteData();
}
client_->setNextBatchToken(response.nextBatch()); client_->setNextBatchToken(response.nextBatch());
QtConcurrent::run(cache_.data(), &Cache::setState, response.nextBatch(), state_manager_);
// Populate timelines with messages. // Populate timelines with messages.
view_manager_->initialize(response.rooms()); view_manager_->initialize(response.rooms());

View file

@ -56,9 +56,9 @@ MatrixClient::MatrixClient(QString server, QObject *parent)
void void
MatrixClient::reset() noexcept MatrixClient::reset() noexcept
{ {
next_batch_ = ""; next_batch_.clear();
server_ = ""; server_.clear();
token_ = ""; token_.clear();
txn_id_ = 0; txn_id_ = 0;
} }