Make Realm::compact() more robust

Throw if it's called on a read-only Realm and ensure the Realm is left in a
valid state regardless of the starting state.
This commit is contained in:
Thomas Goyne 2015-09-04 11:34:02 -07:00
parent 6133eebf8b
commit b381437a45
1 changed files with 7 additions and 6 deletions

View File

@ -291,20 +291,21 @@ bool Realm::compact()
{ {
verify_thread(); verify_thread();
bool success = false; if (m_config.read_only) {
throw InvalidTransactionException("Can't compact a read-only Realm");
}
if (m_in_transaction) { if (m_in_transaction) {
throw InvalidTransactionException("Can't compact a Realm within a write transaction"); throw InvalidTransactionException("Can't compact a Realm within a write transaction");
} }
Group* group = read_group();
for (auto &object_schema : *m_config.schema) { for (auto &object_schema : *m_config.schema) {
ObjectStore::table_for_object_type(read_group(), object_schema.name)->optimize(); ObjectStore::table_for_object_type(group, object_schema.name)->optimize();
} }
m_shared_group->end_read(); m_shared_group->end_read();
success = m_shared_group->compact(); m_group = nullptr;
m_shared_group->begin_read();
return success; return m_shared_group->compact();
} }
void Realm::notify() void Realm::notify()