     }
     
     open func setValue(_ value: [String : Any]?, forKey key: String) {
-        guard let db = db else {
-            NSLog("Database reference is not existent (it has probably been closed)")
-            return
-        }
-        if let newValue = value {
-            var status = 0
-            if var data = encoder(key, newValue) {
-                data.withUnsafeMutableBytes { (mutableBytes: UnsafeMutablePointer<UInt8>) -> () in
-                    status = levelDBItemPut(db, key.cString, key.count, mutableBytes, data.count)
-                }
-                if status != 0 {
-                    NSLog("setValue: Problem storing key/value pair in database")
+        levelDBQueue.sync {
+            guard let db = db else {
+                NSLog("Database reference is not existent (it has probably been closed)")
+                return
+            }
+            if let newValue = value {
+                var status = 0
+                if var data = encoder(key, newValue) {
+                    data.withUnsafeMutableBytes { (mutableBytes: UnsafeMutablePointer<UInt8>) -> () in
+                        status = levelDBItemPut(db, key.cString, key.count, mutableBytes, data.count)
+                    }
+                    if status != 0 {
+                        NSLog("setValue: Problem storing key/value pair in database")
+                    }
+                } else {
+                    NSLog("Error: setValue: encoder(key, newValue) returned nil, key: \(key), newValue: \(newValue)")
                 }
             } else {
-                NSLog("Error: setValue: encoder(key, newValue) returned nil, key: \(key), newValue: \(newValue)")
-            }
-        } else {
-            //NSLog("setValue: newValue is nil")
-            levelDBItemDelete(db, key.cString, key.count)
+                //NSLog("setValue: newValue is nil")
+                levelDBItemDelete(db, key.cString, key.count)
+            }
         }
     }
     

    }
    
    open func removeAllValuesWithPrefix(_ prefix: String) {
        guard let db = db else {
            NSLog("Database reference is not existent (it has probably been closed)")
            return
        }
        let iterator = levelDBIteratorNew(db)
        let prefixPtr = prefix.cString
        let prefixLen = prefix.count
        
        if prefixLen > 0 {
            levelDBIteratorSeek(iterator, prefix.cString, prefixLen)
        } else {
            levelDBIteratorMoveToFirst(iterator)
        }
        while levelDBIteratorIsValid(iterator) {
            var iKey: UnsafeMutablePointer<Int8>? = nil
            var iKeyLength: Int = 0
            levelDBIteratorGetKey(iterator, &iKey, &iKeyLength)
            if let iKey = iKey {
                if prefixLen > 0 {
                    if memcmp(iKey, prefixPtr, min(prefixLen, iKeyLength)) != 0 {
                        break;
                    }
                }
            }
            levelDBItemDelete(db, iKey, iKeyLength)
            levelDBIteratorMoveForward(iterator)
        }
        levelDBIteratorDelete(iterator)
    }
    
    open func allKeys() -> [String] {

    return [""]
}

public let levelDBQueue = DispatchQueue(label: "com.twister.leveldb")

open class LevelDB {
    
    var name: String
