From 063754f6530d89f7f3fd16023ba81ed65654e788 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Mon, 20 Mar 2023 15:02:29 +0100 Subject: [PATCH] Parallelise conversion of NKFiles to NextclkoudItemMetadataTables Signed-off-by: Claudio Cambra --- .../NextcloudItemMetadataTable+NKFile.swift | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudItemMetadataTable+NKFile.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudItemMetadataTable+NKFile.swift index 305d9cb0b6004..fa7860364366f 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudItemMetadataTable+NKFile.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudItemMetadataTable+NKFile.swift @@ -98,21 +98,30 @@ extension NextcloudItemMetadataTable { var childDirectoriesMetadatas: [NextcloudItemMetadataTable] = [] var metadatas: [NextcloudItemMetadataTable] = [] + let conversionQueue = DispatchQueue(label: "nkFileToMetadataConversionQueue", qos: .userInitiated, attributes: .concurrent) + let appendQueue = DispatchQueue(label: "metadataAppendQueue", qos: .userInitiated) + let dispatchGroup = DispatchGroup() + for file in files { - let metadata = NextcloudItemMetadataTable.fromNKFile(file, account: account) + conversionQueue.async(group: dispatchGroup) { + let metadata = NextcloudItemMetadataTable.fromNKFile(file, account: account) - if metadatas.isEmpty && !directoryMetadataSet { - assert(metadata.directory) // In directory read first metadata will always be a dir - directoryMetadata = metadata; - directoryMetadataSet = true; - } else { - metadatas.append(metadata) - if metadata.directory { - childDirectoriesMetadatas.append(metadata) + if metadatas.isEmpty && !directoryMetadataSet { + directoryMetadata = metadata; + directoryMetadataSet = true; + } else { + appendQueue.async(group: dispatchGroup) { + metadatas.append(metadata) + if metadata.directory { + childDirectoriesMetadatas.append(metadata) + } + } } } } - completionHandler(directoryMetadata, childDirectoriesMetadatas, metadatas) + dispatchGroup.notify(queue: DispatchQueue.main) { + completionHandler(directoryMetadata, childDirectoriesMetadatas, metadatas) + } } }