diff --git a/MTMR/CurrencyBarItem.swift b/MTMR/CurrencyBarItem.swift index 0fe6b7b..5f97a62 100644 --- a/MTMR/CurrencyBarItem.swift +++ b/MTMR/CurrencyBarItem.swift @@ -10,8 +10,7 @@ import Cocoa import CoreLocation class CurrencyBarItem: CustomButtonTouchBarItem { - private var timer: Timer! - private var interval: TimeInterval! + private let activity = NSBackgroundActivityScheduler(identifier: "com.toxblh.mtmr.currency.updatecheck") private var prefix: String private var from: String private var to: String @@ -38,7 +37,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem { ] init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, from: String, to: String, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { - self.interval = interval + activity.interval = interval self.from = from self.to = to @@ -52,8 +51,12 @@ class CurrencyBarItem: CustomButtonTouchBarItem { self.view = button - timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(updateCurrency), userInfo: nil, repeats: true) - + activity.repeats = true + activity.qualityOfService = .utility + activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in + self.updateCurrency() + completion(NSBackgroundActivityScheduler.Result.finished) + } updateCurrency() } @@ -68,7 +71,7 @@ class CurrencyBarItem: CustomButtonTouchBarItem { if error == nil { do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] - +// print(json) var value: Float32! if let data_array = json["data"] as? [String : AnyObject] { diff --git a/MTMR/WeatherBarItem.swift b/MTMR/WeatherBarItem.swift index 4104027..4079532 100644 --- a/MTMR/WeatherBarItem.swift +++ b/MTMR/WeatherBarItem.swift @@ -10,9 +10,7 @@ import Cocoa import CoreLocation class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { - private let dateFormatter = DateFormatter() - private var timer: Timer! - private var interval: TimeInterval! + private let activity = NSBackgroundActivityScheduler(identifier: "com.toxblh.mtmr.weather.updatecheck") private var units: String private var api_key: String private var units_str = "°F" @@ -25,7 +23,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { private var manager:CLLocationManager! init(identifier: NSTouchBarItem.Identifier, interval: TimeInterval, units: String, api_key: String, icon_type: String? = "text", onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { - self.interval = interval + activity.interval = interval self.units = units self.api_key = api_key @@ -57,8 +55,14 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { print("Location services not enabled"); return } - - timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(updateWeather), userInfo: nil, repeats: true) + + activity.repeats = true + activity.qualityOfService = .utility + activity.schedule { (completion: NSBackgroundActivityScheduler.CompletionHandler) in + self.updateWeather() + completion(NSBackgroundActivityScheduler.Result.finished) + } + updateWeather() manager = CLLocationManager() manager.delegate = self @@ -79,7 +83,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { if error == nil { do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject] - +// print(json) var temperature: Int! var condition_icon = ""