From b4d8290bd68ebb1669eb9605b3a3fc694bfc23b7 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 NextcloudItemMetadataTables Signed-off-by: Claudio Cambra --- .../NextcloudItemMetadataTable+NKFile.swift | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 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..29683e7220c68 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudItemMetadataTable+NKFile.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudItemMetadataTable+NKFile.swift @@ -98,21 +98,31 @@ extension NextcloudItemMetadataTable { var childDirectoriesMetadatas: [NextcloudItemMetadataTable] = [] var metadatas: [NextcloudItemMetadataTable] = [] - for file in files { - let metadata = NextcloudItemMetadataTable.fromNKFile(file, account: account) + let conversionQueue = DispatchQueue(label: "nkFileToMetadataConversionQueue", qos: .userInitiated, attributes: .concurrent) + let appendQueue = DispatchQueue(label: "metadataAppendQueue", qos: .userInitiated) // Serial queue + let dispatchGroup = DispatchGroup() + for file in files { if metadatas.isEmpty && !directoryMetadataSet { - assert(metadata.directory) // In directory read first metadata will always be a dir + let metadata = NextcloudItemMetadataTable.fromNKFile(file, account: account) directoryMetadata = metadata; directoryMetadataSet = true; } else { - metadatas.append(metadata) - if metadata.directory { - childDirectoriesMetadatas.append(metadata) + conversionQueue.async(group: dispatchGroup) { + let metadata = NextcloudItemMetadataTable.fromNKFile(file, account: account) + + 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) + } } }