Commit 6045c804 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk

fix: database connection

Protect database access when initialize connections

Change-Id: I7e536562dc8a58728b4148f6bac2b782484c51c9
parent 4845e64a
......@@ -30,6 +30,7 @@ enum DataAccessError: Error {
final class DBContainer {
var jamiDB: Connection?
private var connections = [String: Connection?]()
var connectionsSemaphore = DispatchSemaphore(value: 1)
private let log = SwiftyBeaver.self
private let jamiDBName = "ring.db"
private let path: String?
......@@ -69,11 +70,13 @@ final class DBContainer {
}
guard let dbPath = path else { return nil }
do {
let accountDb = try Connection("\(dbPath)/" + "\(account).db")
accountDb.userVersion = dbVersion
connections[account] = accountDb
return accountDb
self.connectionsSemaphore.wait()
connections[account] = try Connection("\(dbPath)/" + "\(account).db")
connections[account]??.userVersion = dbVersion
self.connectionsSemaphore.signal()
return connections[account] ?? nil
} catch {
self.connectionsSemaphore.signal()
log.error("Unable to open database")
return nil
}
......
......@@ -277,11 +277,11 @@ class DBManager {
guard let dataBase = self.dbConnections.forAccount(account: accountId) else {
throw DBBridgingError.getConversationFailed
}
try dataBase.transaction {
try dataBase.transaction(Connection.TransactionMode.immediate, block: {
let conversations = try self.buildConversationsForAccount(accountId: accountId)
observable.onNext(conversations)
observable.on(.completed)
}
})
} catch {
observable.on(.error(DBBridgingError.getConversationFailed))
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment