Don't create background queries for Results on threads without runloops

This commit is contained in:
Thomas Goyne 2015-12-16 11:53:04 -08:00
parent 3e90c30571
commit a95eb50915
4 changed files with 20 additions and 1 deletions

View File

@ -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() { }

View File

@ -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);
}

View File

@ -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);

View File

@ -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();