     /// - Throws: File related errors
     /// - Returns: An array of UploadMetadata types
     func loadAllMetadata() throws -> [UploadMetadata] {
-        let directoryContents = try FileManager.default.contentsOfDirectory(at: storageDirectory, includingPropertiesForKeys: nil)
-        
-        // if you want to filter the directory contents you can do like this:
-        let files = directoryContents.filter{ $0.pathExtension == "plist" }
-        let decoder = PropertyListDecoder()
-        
-        return files.compactMap { url in
-            if let data = try? Data(contentsOf: url) {
-                let metaData = try? decoder.decode(UploadMetadata.self, from: data)
-                        
-                // The documentsDirectory can change between restarts (at least during testing). So we update the filePath to match the existing plist again. To avoid getting an out of sync situation where the filePath still points to a dir in a different directory than the plist.
-                // (The plist and file to upload should always be in the same dir together).
-                metaData?.filePath = url.deletingPathExtension()
+        try queue.sync {
+
+            let directoryContents = try FileManager.default.contentsOfDirectory(at: storageDirectory, includingPropertiesForKeys: nil)
+            
+            // if you want to filter the directory contents you can do like this:
+            let files = directoryContents.filter{ $0.pathExtension == "plist" }
+            let decoder = PropertyListDecoder()
+            
+            let metaData: [UploadMetadata] = files.compactMap { url in
+                if let data = try? Data(contentsOf: url) {
+                    let metaData = try? decoder.decode(UploadMetadata.self, from: data)
+                    
+                    // The documentsDirectory can change between restarts (at least during testing). So we update the filePath to match the existing plist again. To avoid getting an out of sync situation where the filePath still points to a dir in a different directory than the plist.
+                    // (The plist and file to upload should always be in the same dir together).
+                    metaData?.filePath = url.deletingPathExtension()
+                    
+                    return metaData
+                }
                 
-                return metaData
-            }
-            
-            // Improvement: Handle error when it can't be decoded?
-            return nil
+                // Improvement: Handle error when it can't be decoded?
+                return nil
+            }
+            
+            return metaData
         }
     }
     

    /// - Returns: The URL of the stored file
    @discardableResult
    func store(data: Data, id: UUID) throws -> URL {
        guard !data.isEmpty else { throw FilesError.dataIsEmpty }
        try makeDirectoryIfNeeded()
        let fileName = id.uuidString
        
        let targetLocation = storageDirectory.appendingPathComponent(fileName)
        try data.write(to: targetLocation)
        return targetLocation
    }
    
    /// Removes metadata and its related file from disk

    /// - Returns: The URL of the location where the metadata is stored.
    @discardableResult
    func encodeAndStore(metaData: UploadMetadata) throws -> URL {
        try queue.sync {
            guard FileManager.default.fileExists(atPath: metaData.filePath.path) else {
                // Could not find the file that's related to this metadata.
                throw FilesError.relatedFileNotFound
            }
            
            let targetLocation = metaData.filePath.appendingPathExtension("plist")
            try self.makeDirectoryIfNeeded()
            
            let encoder = PropertyListEncoder()
            let encodedData = try encoder.encode(metaData)
            try encodedData.write(to: targetLocation, options: .atomic)
            return targetLocation
        }
    }
    
    /// Load metadata from store and find matching one by id
