Skip to content

Commit

Permalink
don't use timer run on mainthread
Browse files Browse the repository at this point in the history
  • Loading branch information
fuji246 committed Apr 27, 2022
1 parent d9ed5fe commit 4d3a8e0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 37 deletions.
4 changes: 4 additions & 0 deletions LomoAgent.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
3A94CC2E22E407A9002207D5 /* utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A94CC2D22E407A9002207D5 /* utils.swift */; };
3AA732872819C584006EAA8A /* CreateUserWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3AA732892819C584006EAA8A /* CreateUserWindow.xib */; };
3AA7328F2819C7A1006EAA8A /* CreateUserWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA7328E2819C7A1006EAA8A /* CreateUserWindow.swift */; };
3AA732922819FFDC006EAA8A /* RepeatingTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA732912819FFDC006EAA8A /* RepeatingTimer.swift */; };
3AC3E846229F9B60004EA1D7 /* HyperlinkTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AC3E845229F9B60004EA1D7 /* HyperlinkTextField.swift */; };
3AE33D3B23244F2E00B81C13 /* rsync in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3A818C152320D36A00B110B3 /* rsync */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
3AE33D3E23244F6400B81C13 /* lomoupg in CopyFiles */ = {isa = PBXBuildFile; fileRef = 3AE33D3C23244F5100B81C13 /* lomoupg */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
Expand Down Expand Up @@ -132,6 +133,7 @@
3AA7328B2819C58A006EAA8A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/CreateUserWindow.strings; sourceTree = "<group>"; };
3AA7328D2819C58A006EAA8A /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/CreateUserWindow.strings"; sourceTree = "<group>"; };
3AA7328E2819C7A1006EAA8A /* CreateUserWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateUserWindow.swift; sourceTree = "<group>"; };
3AA732912819FFDC006EAA8A /* RepeatingTimer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RepeatingTimer.swift; sourceTree = "<group>"; };
3AC3E845229F9B60004EA1D7 /* HyperlinkTextField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HyperlinkTextField.swift; sourceTree = "<group>"; };
3AE33D3C23244F5100B81C13 /* lomoupg */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = lomoupg; path = dependencies/lomod/Contents/MacOS/lomoupg; sourceTree = "<group>"; };
3AE5886524569FD40079CB36 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/UserWindow.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -261,6 +263,7 @@
3A3896FA222F77FA004A2001 /* LomoAgent */ = {
isa = PBXGroup;
children = (
3AA732912819FFDC006EAA8A /* RepeatingTimer.swift */,
3A1436502320C86700E0F154 /* LomoUpgrade.swift */,
3A3896FB222F77FA004A2001 /* AppDelegate.swift */,
3A2FE3FF22E2B339000022B0 /* QRImage.swift */,
Expand Down Expand Up @@ -473,6 +476,7 @@
files = (
3AA7328F2819C7A1006EAA8A /* CreateUserWindow.swift in Sources */,
3A2FE40022E2B339000022B0 /* QRImage.swift in Sources */,
3AA732922819FFDC006EAA8A /* RepeatingTimer.swift in Sources */,
3AE5886C2456A0F60079CB36 /* UserWindow.swift in Sources */,
3A8BEF1122E4BA0B00DAE39C /* LomodService.swift in Sources */,
3A94CC2E22E407A9002207D5 /* utils.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions LomoAgent/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2d7d634</string>
<string>d9ed5fe</string>
<key>CFBundleVersion</key>
<string>2022_04_27.15_19_46.0.2d7d634</string>
<string>2022_04_27.15_58_01.0.d9ed5fe</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
3 changes: 2 additions & 1 deletion LomoAgent/LomodService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LomodService

private(set) var members = [Member]()

private var systemInfo: SystemInfo?
private(set) var systemInfo: SystemInfo?

private(set) public var finalBackupTime: String = ""

Expand Down Expand Up @@ -312,6 +312,7 @@ class LomodService
}

func checkServerStatus(completionHandler: @escaping (SystemInfo?, Error?) -> Swift.Void) {
systemInfo = nil
var networkError: Error?
if let port = UserDefaults.standard.string(forKey: PREF_LOMOD_PORT) {
if let url = URL(string: "http://\(LOCAL_HOST):\(port)/system"),
Expand Down
64 changes: 64 additions & 0 deletions LomoAgent/RepeatingTimer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// RepeatingTimer.swift
// Pods
//
// Created by jeromy on 4/27/22.
//
import Foundation

/// RepeatingTimer mimics the API of DispatchSourceTimer but in a way that prevents
/// crashes that occur from calling resume multiple times on a timer that is
/// already resumed (noted by https://github.com/SiftScience/sift-ios/issues/52
class RepeatingTimer {

let timeInterval: TimeInterval

init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
}

private lazy var timer: DispatchSourceTimer = {
let t = DispatchSource.makeTimerSource()
t.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval)
t.setEventHandler(handler: { [weak self] in
self?.eventHandler?()
})
return t
}()

var eventHandler: (() -> Void)?

private enum State {
case suspended
case resumed
}

private var state: State = .suspended

deinit {
timer.setEventHandler {}
timer.cancel()
/*
If the timer is suspended, calling cancel without resuming
triggers a crash. This is documented here https://forums.developer.apple.com/thread/15902
*/
resume()
eventHandler = nil
}

func resume() {
if state == .resumed {
return
}
state = .resumed
timer.resume()
}

func suspend() {
if state == .suspended {
return
}
state = .suspended
timer.suspend()
}
}
23 changes: 14 additions & 9 deletions LomoAgent/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ class StatusMenuController: NSObject {
var aboutWindow: AboutWindow!
var lomodTask: Process?
var lomoWebTask: Process?
var stateTimer: Timer!
var pingTimer: Timer = Timer()
var updateTimer: Timer = Timer()
var guideUrlPoped = false
static let stateTimerIntervalSec = 1.0
static let pingTimerIntervalSec = 10.0
let stateReaptingTimer = RepeatingTimer(timeInterval: 1)
let pingReaptingTimer = RepeatingTimer(timeInterval: 5)
static let autoUpdateHour = 4
static let autoUpdateMinute = 0

Expand Down Expand Up @@ -107,8 +105,8 @@ class StatusMenuController: NSObject {
fileprivate func doExit() {
stopLomoService()
NotificationCenter.default.removeObserver(self)
stateTimer.invalidate()
pingTimer.invalidate()
stateReaptingTimer.suspend()
pingReaptingTimer.suspend()
updateTimer.invalidate()
NSApplication.shared.terminate(self)
}
Expand Down Expand Up @@ -166,8 +164,10 @@ class StatusMenuController: NSObject {
name: .NotifyExit,
object: nil)

stateTimer = Timer.scheduledTimer(timeInterval: StatusMenuController.stateTimerIntervalSec, target: self, selector: #selector(checkLomodState), userInfo: nil, repeats: true)
pingTimer = Timer.scheduledTimer(timeInterval: StatusMenuController.pingTimerIntervalSec, target: self, selector: #selector(pingLomod), userInfo: nil, repeats: true)
stateReaptingTimer.eventHandler = checkLomodState
pingReaptingTimer.eventHandler = pingLomod
stateReaptingTimer.resume()
pingReaptingTimer.resume()

scheduleAutoUpdate()
}
Expand Down Expand Up @@ -200,7 +200,11 @@ class StatusMenuController: NSObject {

@objc func checkLomodState() {
if let task = lomodTask, task.isRunning {
restartMenuItem.image = NSImage(named: NSImage.statusAvailableName)
if let lomodService = getLomodService(), lomodService.systemInfo != nil {
restartMenuItem.image = NSImage(named: NSImage.statusAvailableName)
} else {
restartMenuItem.image = NSImage(named: NSImage.statusPartiallyAvailableName)
}
} else {
restartMenuItem.image = NSImage(named: NSImage.statusUnavailableName)
if lomodTask != nil {
Expand All @@ -214,6 +218,7 @@ class StatusMenuController: NSObject {

lomodService.checkServerStatus { (systemInfo, connectErr) in
if connectErr == nil {
DDLogError("pingLomod succ!")

DispatchQueue.main.async {
lomodService.getUserList()
Expand Down
Loading

0 comments on commit 4d3a8e0

Please sign in to comment.