Make it possible to disable the commit notifier background worker

This makes it much easier to write tests which test the work done on the
background thread.
This commit is contained in:
Thomas Goyne 2016-02-25 11:19:00 -08:00
parent cfc88b6fd5
commit b7b2822082
3 changed files with 34 additions and 11 deletions

View File

@ -66,7 +66,7 @@ std::shared_ptr<Realm> RealmCoordinator::get_realm(Realm::Config config)
std::lock_guard<std::mutex> lock(m_realm_mutex);
if ((!m_config.read_only && !m_notifier) || (m_config.read_only && m_weak_realm_notifiers.empty())) {
m_config = config;
if (!config.read_only && !m_notifier) {
if (!config.read_only && !m_notifier && config.automatic_change_notifications) {
try {
m_notifier = std::make_unique<ExternalCommitHelper>(*this);
}
@ -202,7 +202,9 @@ void RealmCoordinator::clear_cache()
void RealmCoordinator::send_commit_notifications()
{
REALM_ASSERT(!m_config.read_only);
m_notifier->notify_others();
if (m_notifier) {
m_notifier->notify_others();
}
}
void RealmCoordinator::pin_version(uint_fast64_t version, uint_fast32_t index)

View File

@ -35,13 +35,14 @@ using namespace realm::_impl;
Realm::Config::Config(const Config& c)
: path(c.path)
, encryption_key(c.encryption_key)
, schema_version(c.schema_version)
, migration_function(c.migration_function)
, read_only(c.read_only)
, in_memory(c.in_memory)
, cache(c.cache)
, disable_format_upgrade(c.disable_format_upgrade)
, encryption_key(c.encryption_key)
, schema_version(c.schema_version)
, migration_function(c.migration_function)
, automatic_change_notifications(c.automatic_change_notifications)
{
if (c.schema) {
schema = std::make_unique<Schema>(*c.schema);

View File

@ -47,20 +47,40 @@ namespace realm {
public:
typedef std::function<void(SharedRealm old_realm, SharedRealm realm)> MigrationFunction;
struct Config
{
struct Config {
std::string path;
bool read_only = false;
bool in_memory = false;
bool cache = true;
bool disable_format_upgrade = false;
// User-supplied encryption key. Must be either empty or 64 bytes.
std::vector<char> encryption_key;
// Optional schema for the file. If nullptr, the existing schema
// from the file opened will be used. If present, the file will be
// migrated to the schema if needed.
std::unique_ptr<Schema> schema;
uint64_t schema_version;
MigrationFunction migration_function;
bool read_only = false;
bool in_memory = false;
// The following are intended for internal/testing purposes and
// should not be publically exposed in binding APIs
// If false, always return a new Realm instance, and don't return
// that Realm instance for other requests for a cached Realm. Useful
// for dynamic Realms and for tests that need multiple instances on
// one thread
bool cache = true;
// Throw an exception rather than automatically upgrading the file
// format. Used by the browser to warn the user that it'll modify
// the file.
bool disable_format_upgrade = false;
// Disable the background worker thread for producing change
// notifications. Useful for tests for those notifications so that
// everything can be done deterministically on one thread, and
// speeds up tests that don't need notifications.
bool automatic_change_notifications = true;
Config();
Config(Config&&);
Config(const Config& c);