summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2017-10-21 21:17:01 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2017-10-21 21:17:01 +0300
commit160fe1d668d9c1fa8f089cec34df29c2ba31a56f (patch)
treef0d9fd1aee271afa8e51aea312ad1ec01ff614dc
parent3cae6c39831fe6b1e9661fb2af7034105a5f289a (diff)
Remove cache updates from the main thread
-rw-r--r--CMakeLists.txt5
-rw-r--r--src/Cache.cc17
-rw-r--r--src/ChatPage.cc25
-rw-r--r--src/MatrixClient.cc6
4 files changed, 27 insertions, 26 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b513e297..6f8c167c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -41,6 +41,7 @@ endif()
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5LinguistTools REQUIRED)
+find_package(Qt5Concurrent REQUIRED)
if (APPLE)
find_package(Qt5MacExtras REQUIRED)
@@ -318,9 +319,9 @@ if (BUILD_TESTS)
endif()
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()
- 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()
set (NHEKO_DEPS ${OS_BUNDLE} ${SRC_FILES} ${UI_HEADERS} ${MOC_HEADERS} ${QRC} ${LANG_QRC} ${QM_SRC})
diff --git a/src/Cache.cc b/src/Cache.cc
index 3f7b141b..010b4aa9 100644
--- a/src/Cache.cc
+++ b/src/Cache.cc
@@ -102,14 +102,21 @@ Cache::setState(const QString &nextBatchToken, const QMap<QString, RoomState> &s
if (!isMounted_)
return;
- auto txn = lmdb::txn::begin(env_);
+ try {
+ auto txn = lmdb::txn::begin(env_);
- setNextBatchToken(txn, nextBatchToken);
+ setNextBatchToken(txn, nextBatchToken);
- for (auto it = states.constBegin(); it != states.constEnd(); it++)
- insertRoomState(txn, it.key(), it.value());
+ for (auto it = states.constBegin(); it != states.constEnd(); it++)
+ 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
diff --git a/src/ChatPage.cc b/src/ChatPage.cc
index 5df8dec2..65fef9de 100644
--- a/src/ChatPage.cc
+++ b/src/ChatPage.cc
@@ -18,6 +18,7 @@
#include <QApplication>
#include <QDebug>
#include <QSettings>
+#include <QtConcurrent>
#include "AvatarProvider.h"
#include "ChatPage.h"
@@ -196,6 +197,11 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
this,
SLOT(initialSyncCompleted(const SyncResponse &)));
connect(client_.data(), &MatrixClient::initialSyncFailed, this, [=](const QString &msg) {
+ if (client_->getHomeServer().isEmpty()) {
+ deleteConfigs();
+ return;
+ }
+
initialSyncFailures += 1;
if (initialSyncFailures >= MAX_INITIAL_SYNC_FAILURES) {
@@ -426,14 +432,7 @@ ChatPage::syncCompleted(const SyncResponse &response)
}
}
- try {
- 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();
- }
+ QtConcurrent::run(cache_.data(), &Cache::setState, response.nextBatch(), state_manager_);
client_->setNextBatchToken(response.nextBatch());
@@ -479,16 +478,10 @@ ChatPage::initialSyncCompleted(const SyncResponse &response)
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());
+ QtConcurrent::run(cache_.data(), &Cache::setState, response.nextBatch(), state_manager_);
+
// Populate timelines with messages.
view_manager_->initialize(response.rooms());
diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc
index 4ececd01..f0b3bd26 100644
--- a/src/MatrixClient.cc
+++ b/src/MatrixClient.cc
@@ -56,9 +56,9 @@ MatrixClient::MatrixClient(QString server, QObject *parent)
void
MatrixClient::reset() noexcept
{
- next_batch_ = "";
- server_ = "";
- token_ = "";
+ next_batch_.clear();
+ server_.clear();
+ token_.clear();
txn_id_ = 0;
}