Skip to content

Commit

Permalink
Don't unconditionally rerun queries when sort/distinct are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Feb 26, 2021
1 parent c65ac09 commit 834c969
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/realm/object-store/impl/results_notifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,16 @@ void ResultsNotifier::run()
return;
}

if (has_run() && m_query->sync_view_if_needed() == m_last_seen_version) {
auto new_versions = m_query->sync_view_if_needed();
m_descriptor_ordering.collect_dependencies(m_query->get_table().unchecked_ptr());
m_descriptor_ordering.get_versions(m_query->get_table()->get_parent_group(), new_versions);
if (has_run() && new_versions == m_last_seen_version) {
// We've run previously and none of the tables involved in the query
// changed so we don't need to rerun the query, but we still need to
// check each object in the results to see if it was modified
if (!any_related_table_was_modified(*m_info))
return;
REALM_ASSERT(m_change.empty());
auto checker = get_modification_checker(*m_info, m_query->get_table());
for (size_t i = 0; i < m_previous_rows.size(); ++i) {
if (checker(m_previous_rows[i])) {
Expand All @@ -171,7 +175,7 @@ void ResultsNotifier::run()
m_run_tv = m_query->find_all();
m_run_tv.apply_descriptor_ordering(m_descriptor_ordering);
m_run_tv.sync_if_needed();
m_last_seen_version = m_run_tv.ObjList::get_dependency_versions();
m_last_seen_version = std::move(new_versions);

calculate_changes();
}
Expand Down
14 changes: 14 additions & 0 deletions test/object-store/results.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,20 @@ TEST_CASE("notifications: results") {
});
REQUIRE_FALSE(notifier->get_tableview(tv));
}

SECTION("modifying a linked table not used for sorting does not rerun the query") {
results = Results(r, table).sort({{"link.value", false}});
_impl::CollectionNotifier::Handle<_impl::ResultsNotifierBase> notifier =
std::make_shared<_impl::ResultsNotifier>(results);
_impl::RealmCoordinator::register_notifier(notifier);
advance_and_notify(*r);
REQUIRE(notifier->get_tableview(tv));

write([&] {
second_linked_to_table->create_object(ObjKey(53));
});
REQUIRE_FALSE(notifier->get_tableview(tv));
}
}
}

Expand Down

0 comments on commit 834c969

Please sign in to comment.