From a95eb509153b1c9a1445717893de43c9cd807f55 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 16 Dec 2015 11:53:04 -0800 Subject: [PATCH] Don't create background queries for Results on threads without runloops --- src/binding_context.hpp | 4 ++++ src/results.cpp | 2 +- src/shared_realm.cpp | 13 +++++++++++++ src/shared_realm.hpp | 2 ++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/binding_context.hpp b/src/binding_context.hpp index 4aa6dd94..d57ce541 100644 --- a/src/binding_context.hpp +++ b/src/binding_context.hpp @@ -70,6 +70,10 @@ class BindingContext { public: virtual ~BindingContext() = default; + // If the user adds a notification handler to the Realm, will it ever + // actually be called? + virtual bool can_deliver_notifications() const noexcept { return true; } + // Called by the Realm when a write transaction is committed to the file by // a different Realm instance (possibly in a different process) virtual void changes_available() { } diff --git a/src/results.cpp b/src/results.cpp index a46e83d5..cba5e1f7 100644 --- a/src/results.cpp +++ b/src/results.cpp @@ -180,7 +180,7 @@ void Results::update_tableview() if (!m_live) { return; } - if (!m_realm->config().read_only && !m_realm->is_in_transaction() && !m_background_query) { + if (!m_background_query && !m_realm->is_in_transaction() && m_realm->can_deliver_notifications()) { m_background_query = std::make_shared<_impl::AsyncQuery>(*this); _impl::RealmCoordinator::register_query(m_background_query); } diff --git a/src/shared_realm.cpp b/src/shared_realm.cpp index ce435ab7..272a3367 100644 --- a/src/shared_realm.cpp +++ b/src/shared_realm.cpp @@ -411,6 +411,19 @@ bool Realm::refresh() return true; } +bool Realm::can_deliver_notifications() const noexcept +{ + if (m_config.read_only) { + return false; + } + + if (m_binding_context && !m_binding_context->can_deliver_notifications()) { + return false; + } + + return true; +} + uint64_t Realm::get_schema_version(const realm::Realm::Config &config) { auto coordinator = RealmCoordinator::get_existing_coordinator(config.path); diff --git a/src/shared_realm.hpp b/src/shared_realm.hpp index 422af4bf..34aa5b1f 100644 --- a/src/shared_realm.hpp +++ b/src/shared_realm.hpp @@ -107,6 +107,8 @@ namespace realm { void verify_thread() const; void verify_in_write() const; + bool can_deliver_notifications() const noexcept; + // Close this Realm and remove it from the cache. Continuing to use a // Realm after closing it will produce undefined behavior. void close();