From 7cd50aed7bf807af451743cd7c92a7409d8932e7 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Tue, 8 Feb 2022 22:19:32 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20retain=20cycle=20due=20to?= =?UTF-8?q?=20threading=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Domain/Watcher/App+ConfigWatch.swift | 27 ++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/phpmon/Domain/Watcher/App+ConfigWatch.swift b/phpmon/Domain/Watcher/App+ConfigWatch.swift index 82a60e1..fa1af7b 100644 --- a/phpmon/Domain/Watcher/App+ConfigWatch.swift +++ b/phpmon/Domain/Watcher/App+ConfigWatch.swift @@ -30,17 +30,22 @@ extension App { func handlePhpConfigWatcher(forceReload: Bool = false) { let url = URL(fileURLWithPath: "\(Paths.etcPath)/php/\(PhpEnv.phpInstall.version.short)") - // Watcher needs to be created - if self.watcher == nil { - startWatcher(url) - } - - // Watcher needs to be updated - if self.watcher.url != url || forceReload { - self.watcher.disable() - self.watcher = nil - Log.info("Watcher has stopped watching files. Starting new one...") - startWatcher(url) + // Check whether the watcher exists and schedule on the main thread + // if we don't consistently do this, the app will create duplicate watchers + // due to timing issues, which creates retain cycles. + DispatchQueue.main.async { + // Watcher needs to be created + if self.watcher == nil { + self.startWatcher(url) + } + + // Watcher needs to be updated + if self.watcher.url != url || forceReload { + self.watcher.disable() + self.watcher = nil + Log.info("Watcher has stopped watching files. Starting new one...") + self.startWatcher(url) + } } }