From d5ce4f29addda4c6cc376a647670ec3fbb472320 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 21 Sep 2022 13:43:06 +0800 Subject: [PATCH] LMMgr // Also use GCD in loadDataModels() per input mode. --- Source/Modules/LMMgr.swift | 92 +++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/Source/Modules/LMMgr.swift b/Source/Modules/LMMgr.swift index f05cb43c..4a3209eb 100644 --- a/Source/Modules/LMMgr.swift +++ b/Source/Modules/LMMgr.swift @@ -71,11 +71,11 @@ public enum LMMgr { } public static func loadDataModelsOnAppDelegate() { - let globalQuene = DispatchQueue.global(qos: .default) + let globalQueue = DispatchQueue.global(qos: .default) var showFinishNotification = false let group = DispatchGroup() group.enter() - DispatchQueue.main.async { + globalQueue.async { if !Self.lmCHT.isCNSDataLoaded { Self.lmCHT.loadCNSData(path: getBundleDataPath("data-cns")) } @@ -102,7 +102,7 @@ public enum LMMgr { message: NSLocalizedString("Loading CHT Core Dict...", comment: "") ) group.enter() - globalQuene.async { + globalQueue.async { loadCoreLanguageModelFile(filenameSansExtension: "data-cht", langModel: &Self.lmCHT) group.leave() } @@ -113,7 +113,7 @@ public enum LMMgr { message: NSLocalizedString("Loading CHS Core Dict...", comment: "") ) group.enter() - globalQuene.async { + globalQueue.async { loadCoreLanguageModelFile(filenameSansExtension: "data-chs", langModel: &Self.lmCHS) group.leave() } @@ -128,47 +128,69 @@ public enum LMMgr { } public static func loadDataModel(_ mode: Shared.InputMode) { + let globalQueue = DispatchQueue.global(qos: .default) + var showFinishNotification = false + let group = DispatchGroup() + group.enter() + globalQueue.async { + switch mode { + case .imeModeCHS: + if !Self.lmCHS.isCNSDataLoaded { + Self.lmCHS.loadCNSData(path: getBundleDataPath("data-cns")) + } + if !Self.lmCHS.isMiscDataLoaded { + Self.lmCHS.loadMiscData(path: getBundleDataPath("data-zhuyinwen")) + } + if !Self.lmCHS.isSymbolDataLoaded { + Self.lmCHS.loadSymbolData(path: getBundleDataPath("data-symbols")) + } + case .imeModeCHT: + if !Self.lmCHT.isCNSDataLoaded { + Self.lmCHT.loadCNSData(path: getBundleDataPath("data-cns")) + } + if !Self.lmCHT.isMiscDataLoaded { + Self.lmCHT.loadMiscData(path: getBundleDataPath("data-zhuyinwen")) + } + if !Self.lmCHT.isSymbolDataLoaded { + Self.lmCHT.loadSymbolData(path: getBundleDataPath("data-symbols")) + } + default: break + } + group.leave() + } switch mode { case .imeModeCHS: - if !Self.lmCHS.isMiscDataLoaded { - Self.lmCHS.loadMiscData(path: getBundleDataPath("data-zhuyinwen")) - } - if !Self.lmCHS.isSymbolDataLoaded { - Self.lmCHS.loadSymbolData(path: getBundleDataPath("data-symbols")) - } - if !Self.lmCHS.isCNSDataLoaded { - Self.lmCHS.loadCNSData(path: getBundleDataPath("data-cns")) - } if !Self.lmCHS.isLanguageModelLoaded { + showFinishNotification = true NotifierController.notify( message: NSLocalizedString("Loading CHS Core Dict...", comment: "") ) - loadCoreLanguageModelFile(filenameSansExtension: "data-chs", langModel: &Self.lmCHS) - NotifierController.notify( - message: NSLocalizedString("Core Dict loading complete.", comment: "") - ) + group.enter() + globalQueue.async { + loadCoreLanguageModelFile(filenameSansExtension: "data-chs", langModel: &Self.lmCHS) + group.leave() + } } case .imeModeCHT: - if !Self.lmCHT.isMiscDataLoaded { - Self.lmCHT.loadMiscData(path: getBundleDataPath("data-zhuyinwen")) - } - if !Self.lmCHT.isSymbolDataLoaded { - Self.lmCHT.loadSymbolData(path: getBundleDataPath("data-symbols")) - } - if !Self.lmCHT.isCNSDataLoaded { - Self.lmCHT.loadCNSData(path: getBundleDataPath("data-cns")) - } if !Self.lmCHT.isLanguageModelLoaded { + showFinishNotification = true NotifierController.notify( message: NSLocalizedString("Loading CHT Core Dict...", comment: "") ) - loadCoreLanguageModelFile(filenameSansExtension: "data-cht", langModel: &Self.lmCHT) - NotifierController.notify( - message: NSLocalizedString("Core Dict loading complete.", comment: "") - ) + group.enter() + globalQueue.async { + loadCoreLanguageModelFile(filenameSansExtension: "data-cht", langModel: &Self.lmCHT) + group.leave() + } } - case .imeModeNULL: - break + default: break + } + group.notify(queue: DispatchQueue.main) { + if showFinishNotification { + NotifierController.notify( + message: NSLocalizedString("Core Dict loading complete.", comment: "") + ) + } } } @@ -576,15 +598,15 @@ public enum LMMgr { // MARK: UOM public static func saveUserOverrideModelData() { - let globalQuene = DispatchQueue.global(qos: .default) + let globalQueue = DispatchQueue.global(qos: .default) let group = DispatchGroup() group.enter() - globalQuene.async { + globalQueue.async { Self.uomCHT.saveData(toURL: userOverrideModelDataURL(.imeModeCHT)) group.leave() } group.enter() - globalQuene.async { + globalQueue.async { Self.uomCHS.saveData(toURL: userOverrideModelDataURL(.imeModeCHS)) group.leave() }