diff --git a/src/impl/results_notifier.cpp b/src/impl/results_notifier.cpp index dcd51b8e..f53f060d 100644 --- a/src/impl/results_notifier.cpp +++ b/src/impl/results_notifier.cpp @@ -73,7 +73,7 @@ bool ResultsNotifier::do_add_required_change_info(TransactionChangeInfo& info) return m_initial_run_complete && have_callbacks(); } -void ResultsNotifier::run() +bool ResultsNotifier::need_to_run() { REALM_ASSERT(m_info); REALM_ASSERT(!m_tv.is_attached()); @@ -82,7 +82,7 @@ void ResultsNotifier::run() auto lock = lock_target(); // Don't run the query if the results aren't actually going to be used if (!get_realm() || (!have_callbacks() && !m_target_results->wants_background_updates())) { - return; + return false; } } @@ -91,16 +91,15 @@ void ResultsNotifier::run() // Make an empty tableview from the query to get the table version, since // Query doesn't expose it if (m_query->find_all(0, 0, 0).sync_if_needed() == m_last_seen_version) { - return; + return false; } } - m_tv = m_query->find_all(); - if (m_sort) { - m_tv.sort(m_sort.column_indices, m_sort.ascending); - } - m_last_seen_version = m_tv.sync_if_needed(); + return true; +} +void ResultsNotifier::calculate_changes() +{ size_t table_ndx = m_query->get_table()->get_index_in_group(); if (m_initial_run_complete) { auto changes = table_ndx < m_info->tables.size() ? &m_info->tables[table_ndx] : nullptr; @@ -129,10 +128,6 @@ void ResultsNotifier::run() m_sort || m_from_linkview); m_previous_rows = std::move(next_rows); - if (m_changes.empty()) { - m_tv = {}; - return; - } } else { m_previous_rows.resize(m_tv.size()); @@ -141,6 +136,20 @@ void ResultsNotifier::run() } } +void ResultsNotifier::run() +{ + if (!need_to_run()) + return; + + m_tv = m_query->find_all(); + if (m_sort) { + m_tv.sort(m_sort.column_indices, m_sort.ascending); + } + m_last_seen_version = m_tv.sync_if_needed(); + + calculate_changes(); +} + void ResultsNotifier::do_prepare_handover(SharedGroup& sg) { if (!m_tv.is_attached()) { diff --git a/src/impl/results_notifier.hpp b/src/impl/results_notifier.hpp index 3dfc81f6..03b4be60 100644 --- a/src/impl/results_notifier.hpp +++ b/src/impl/results_notifier.hpp @@ -62,6 +62,9 @@ private: // can lead to deliver() being called before that bool m_initial_run_complete = false; + bool need_to_run(); + void calculate_changes(); + void run() override; void do_prepare_handover(SharedGroup&) override; bool do_deliver(SharedGroup& sg) override;