Actually remove the Realm from the cache when close() is called
This commit is contained in:
parent
9b8a0d5346
commit
89bd55a535
|
@ -25,6 +25,7 @@ using namespace realm::_impl;
|
|||
|
||||
CachedRealm::CachedRealm(const std::shared_ptr<Realm>& realm, bool cache)
|
||||
: m_realm(realm)
|
||||
, m_realm_key(realm.get())
|
||||
, m_cache(cache)
|
||||
{
|
||||
struct RefCountedWeakPointer {
|
||||
|
|
|
@ -45,14 +45,22 @@ public:
|
|||
// Does this CachedRealm store a Realm instance that should be used on the current thread?
|
||||
bool is_cached_for_current_thread() const { return m_cache && m_thread_id == std::this_thread::get_id(); }
|
||||
|
||||
// Has the Realm instance been destroyed?
|
||||
bool expired() const { return m_realm.expired(); }
|
||||
|
||||
// Asyncronously call notify() on the Realm on the appropriate thread
|
||||
void notify();
|
||||
|
||||
// Is this a CachedRealm for the given Realm instance?
|
||||
// This should be used rather than lock() to avoid deadlocks when the
|
||||
// reference from lock() is the last remaining one (due to another thread
|
||||
// releasing them at the same time)
|
||||
bool is_for_realm(Realm* realm) const { return realm == m_realm_key; }
|
||||
|
||||
private:
|
||||
std::weak_ptr<Realm> m_realm;
|
||||
std::thread::id m_thread_id = std::this_thread::get_id();
|
||||
void* m_realm_key;
|
||||
bool m_cache = false;
|
||||
|
||||
CFRunLoopRef m_runloop;
|
||||
|
|
|
@ -122,16 +122,19 @@ RealmCoordinator::~RealmCoordinator()
|
|||
}
|
||||
}
|
||||
|
||||
void RealmCoordinator::unregister_realm(Realm*)
|
||||
void RealmCoordinator::unregister_realm(Realm* realm)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_realm_mutex);
|
||||
for (size_t i = 0; i < m_cached_realms.size(); ++i) {
|
||||
if (m_cached_realms[i].expired()) {
|
||||
if (i + 1 < m_cached_realms.size()) {
|
||||
m_cached_realms[i] = std::move(m_cached_realms.back());
|
||||
}
|
||||
m_cached_realms.pop_back();
|
||||
auto& cached_realm = m_cached_realms[i];
|
||||
if (!cached_realm.expired() && !cached_realm.is_for_realm(realm)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i + 1 < m_cached_realms.size()) {
|
||||
cached_realm = std::move(m_cached_realms.back());
|
||||
}
|
||||
m_cached_realms.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue