From 2672cd901f3e21f90c4800ccbc9b06e76a2811bb Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Fri, 20 May 2016 13:42:17 -0700 Subject: [PATCH] Fix crash and other errors with Results snapshots When deleteAll() is called on a Realm, it calls clear() on all of its Tables, which seems to not update TableViews unless they are synced. The is_row_attached(row_ndx) method still returns true otherwise. A workaround is here until that is fixed. Fixes #434 --- src/results.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/results.cpp b/src/results.cpp index df4679a2..cca9acf1 100644 --- a/src/results.cpp +++ b/src/results.cpp @@ -96,7 +96,7 @@ bool Results::is_valid() const m_realm->verify_thread(); if (m_table && !m_table->is_attached()) return false; - if (m_mode == Mode::TableView && (!m_table_view.is_attached() || m_table_view.depends_on_deleted_object())) + if (m_mode == Mode::TableView && (!m_table_view.is_attached() || (m_live && m_table_view.depends_on_deleted_object()))) return false; if (m_mode == Mode::LinkView && !m_link_view->is_attached()) return false; @@ -121,8 +121,8 @@ void Results::set_live(bool live) { validate_read(); - if (!live && m_mode == Mode::Table) { - m_query = m_table->where(); + if (!live && (m_mode == Mode::Table || m_mode == Mode::LinkView)) { + m_query = get_query(); m_mode = Mode::Query; } @@ -173,7 +173,8 @@ RowExpr Results::get(size_t row_ndx) update_tableview(); if (row_ndx >= m_table_view.size()) break; - if (!m_live && !m_table_view.is_row_attached(row_ndx)) + // If clear() was called on the underlying Table, then is_row_attached(row_ndx) will still return true. + if (!m_live && (m_table_view.get_parent().is_empty() || !m_table_view.is_row_attached(row_ndx))) return {}; return m_table_view.get(row_ndx); } @@ -392,7 +393,15 @@ void Results::clear() case Mode::TableView: validate_write(); update_tableview(); - m_table_view.clear(RemoveMode::unordered); + + if (m_live) { + m_table_view.clear(RemoveMode::unordered); + } + else { + // Copy the TableView because a non-live Results shouldn't have let its size() change. + TableView table_view_copy = m_table_view; + table_view_copy.clear(RemoveMode::unordered); + } break; case Mode::LinkView: validate_write();