diff --git a/Ring/Ring/Database/DBContainer.swift b/Ring/Ring/Database/DBContainer.swift index bd907abef5503e8f999a2255f66065faefaf35cd..a98b6d1f115b105d2801daeb7dd837474b47ae17 100644 --- a/Ring/Ring/Database/DBContainer.swift +++ b/Ring/Ring/Database/DBContainer.swift @@ -54,29 +54,34 @@ final class DBContainer { private let dbVersions = [1, 2] func removeDBForAccount(account: String, removeFolder: Bool) { + self.connectionsSemaphore.wait() connections[account] = nil + self.connectionsSemaphore.signal() if !removeFolder { return } self.removeAccountFolder(accountId: account) } func removeDBForAccount(account: String) { + self.connectionsSemaphore.wait() connections[account] = nil + self.connectionsSemaphore.signal() self.removeDBNamed(dbName: "\(account).db") } func forAccount(account: String) -> Connection? { + self.connectionsSemaphore.wait() + defer { + self.connectionsSemaphore.signal() + } if connections[account] != nil { return connections[account] ?? nil } guard let dbPath = accountDbPath(accountId: account) else { return nil } do { - self.connectionsSemaphore.wait() connections[account] = try Connection(dbPath) connections[account]??.userVersion = dbVersions.last - self.connectionsSemaphore.signal() return connections[account] ?? nil } catch { - self.connectionsSemaphore.signal() log.error("Unable to open database") return nil }