From 2723fe541aa7ee0b474483ea0f1c4862ce67d473 Mon Sep 17 00:00:00 2001 From: Kye Maloy Date: Wed, 1 Aug 2018 16:20:35 +0100 Subject: [PATCH] fix: improves handling on concurrent accesses to codableMap (#64) * fix: improves handling on concurrent accesses to codableMap * added Dispatch import for (_ idColumn: (name: String, type: SQLDataType.Type), _ tableName: String, for type: T.Type) throws -> Table { @@ -30,11 +34,19 @@ public class TableInfo { } func getInfo(_ idColumn: (name: String, type: SQLDataType.Type), _ tableName: String, _ type: T.Type) throws -> (info: TypeInfo, table: Table) { - if codableMap["\(type)"] == nil { - let typeInfo = try TypeDecoder.decode(type) - codableMap["\(type)"] = (info: typeInfo, table: try constructTable(idColumn, tableName, typeInfo)) + let typeString = "\(type)" + if let result = codableMap[typeString] { + return result } - return codableMap["\(type)"]! + + codableMapLock.wait() + defer { codableMapLock.signal() } + + let typeInfo = try TypeDecoder.decode(type) + let result = (info: typeInfo, table: try constructTable(idColumn, tableName, typeInfo)) + codableMap[typeString] = result + + return result } /// Construct the table for a Model