From 2a22117badbeabbf912acc52b5b04c732e237fbb Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Wed, 21 Sep 2022 14:33:00 +0800 Subject: [PATCH] LMMgr // Use GCD in loadDataModelsOnAppDelegate(). --- Source/Modules/AppDelegate.swift | 1 + Source/Modules/LMMgr.swift | 70 +++++++++++++++--------- Source/Modules/ctlInputMethod_Core.swift | 2 +- 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/Source/Modules/AppDelegate.swift b/Source/Modules/AppDelegate.swift index 447ffd26..aac63946 100644 --- a/Source/Modules/AppDelegate.swift +++ b/Source/Modules/AppDelegate.swift @@ -51,6 +51,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele NSUserNotificationCenter.default.deliver(userNotification) } + LMMgr.loadDataModelsOnAppDelegate() DispatchQueue.main.async { LMMgr.initUserLangModels() self.folderMonitor.folderDidChange = { [weak self] in diff --git a/Source/Modules/LMMgr.swift b/Source/Modules/LMMgr.swift index 0673c6e8..f05cb43c 100644 --- a/Source/Modules/LMMgr.swift +++ b/Source/Modules/LMMgr.swift @@ -70,42 +70,60 @@ public enum LMMgr { lm.loadLanguageModel(path: dataPath) } - public static func loadDataModels() { - 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")) - } - 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")) + public static func loadDataModelsOnAppDelegate() { + let globalQuene = DispatchQueue.global(qos: .default) + var showFinishNotification = false + let group = DispatchGroup() + group.enter() + DispatchQueue.main.async { + 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")) + } + 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")) + } + group.leave() } 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() + globalQuene.async { + loadCoreLanguageModelFile(filenameSansExtension: "data-cht", langModel: &Self.lmCHT) + group.leave() + } } 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() + globalQuene.async { + loadCoreLanguageModelFile(filenameSansExtension: "data-chs", langModel: &Self.lmCHS) + group.leave() + } + } + group.notify(queue: DispatchQueue.main) { + if showFinishNotification { + NotifierController.notify( + message: NSLocalizedString("Core Dict loading complete.", comment: "") + ) + } } } diff --git a/Source/Modules/ctlInputMethod_Core.swift b/Source/Modules/ctlInputMethod_Core.swift index 48c648bb..43784396 100644 --- a/Source/Modules/ctlInputMethod_Core.swift +++ b/Source/Modules/ctlInputMethod_Core.swift @@ -177,7 +177,7 @@ class ctlInputMethod: IMKInputController { default: newInputMode = .imeModeNULL } - LMMgr.loadDataModel(newInputMode) + // LMMgr.loadDataModel(newInputMode) if inputMode != newInputMode { UserDefaults.standard.synchronize()