From 6bb25782c178d382349e729615011b414e671354 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 15 Nov 2018 13:37:59 +0530 Subject: Exporting: Export to zip in a background thread Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/ZipArchive/ZipExporter.swift | 35 +++++++++++++++--------- 1 file changed, 22 insertions(+), 13 deletions(-) (limited to 'WireGuard/WireGuard/ZipArchive') diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift index d0cf9a7..bd6c3dd 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift @@ -8,21 +8,30 @@ enum ZipExporterError: Error { } class ZipExporter { - static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to destinationURL: URL) throws { + static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to url: URL, completion: @escaping (Error?) -> Void) { - guard (!tunnelConfigurations.isEmpty) else { throw ZipExporterError.noTunnelsToExport } - - var inputsToArchiver: [(fileName: String, contents: Data)] = [] - - var lastTunnelName: String = "" - for tunnelConfiguration in tunnelConfigurations { - if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) { - let name = tunnelConfiguration.interface.name - if (name.isEmpty || name == lastTunnelName) { continue } - inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) - lastTunnelName = name + guard (!tunnelConfigurations.isEmpty) else { + completion(ZipExporterError.noTunnelsToExport) + return + } + DispatchQueue.global(qos: .userInitiated).async { + var inputsToArchiver: [(fileName: String, contents: Data)] = [] + var lastTunnelName: String = "" + for tunnelConfiguration in tunnelConfigurations { + if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) { + let name = tunnelConfiguration.interface.name + if (name.isEmpty || name == lastTunnelName) { continue } + inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) + lastTunnelName = name + } + } + do { + try ZipArchive.archive(inputs: inputsToArchiver, to: url) + } catch (let e) { + DispatchQueue.main.async { completion(e) } + return } + DispatchQueue.main.async { completion(nil) } } - try ZipArchive.archive(inputs: inputsToArchiver, to: destinationURL) } } -- cgit v1.2.3-59-g8ed1b