burn some leaks

This commit is contained in:
Tomas Harkema
2021-04-27 16:19:35 +02:00
committed by Alexander Heinrich
parent 9f41994380
commit f8fb99cc41
5 changed files with 24 additions and 20 deletions

View File

@@ -32,7 +32,8 @@ class FindMyController: ObservableObject {
self.devices = devices
// Decrypt the reports with the imported keys
DispatchQueue.global(qos: .background).async {
DispatchQueue.global(qos: .background).async { [weak self] in
guard let self = self else { return }
var d = self.devices
// Add the reports to the according device by finding the right key for the report
@@ -57,8 +58,8 @@ class FindMyController: ObservableObject {
}
// Decrypt the reports
self.decryptReports {
self.exportDevices()
self.decryptReports { [weak self] in
self?.exportDevices()
DispatchQueue.main.async {
completion()
}
@@ -108,7 +109,8 @@ class FindMyController: ObservableObject {
func fetchReports(with searchPartyToken: Data, completion: @escaping (Error?) -> Void) {
DispatchQueue.global(qos: .background).async {
DispatchQueue.global(qos: .background).async { [weak self] in
guard let self = self else { return }
let fetchReportGroup = DispatchGroup()
let fetcher = ReportsFetcher()
@@ -166,10 +168,10 @@ class FindMyController: ObservableObject {
}
#endif
DispatchQueue.main.async {
self.devices = devices
DispatchQueue.main.async { [weak self] in
self?.devices = devices
self.decryptReports {
self?.decryptReports {
completion(nil)
}

View File

@@ -30,12 +30,12 @@ class AccessoryController: ObservableObject {
}
func initAccessoryObserver() {
self.selfObserver = self.objectWillChange.sink { _ in
self.selfObserver = self.objectWillChange.sink { [weak self] _ in
// objectWillChange is called before the values are actually changed,
// so we dispatch the call to save()
DispatchQueue.main.async {
self.initObserver()
try? self.save()
DispatchQueue.main.async { [weak self] in
self?.initObserver()
try? self?.save()
}
}
}
@@ -45,7 +45,7 @@ class AccessoryController: ObservableObject {
$0.cancel()
})
self.accessories.forEach({
let c = $0.objectWillChange.sink(receiveValue: { self.objectWillChange.send() })
let c = $0.objectWillChange.sink(receiveValue: { [weak self] in self?.objectWillChange.send() })
// Important: You have to keep the returned value allocated,
// otherwise the sink subscription gets cancelled
self.listElementsObserver.append(c)
@@ -160,7 +160,8 @@ class AccessoryController: ObservableObject {
///
/// - Parameter completion: called when the reports have been succesfully downloaded or the request has failed
func downloadLocationReports(completion: @escaping (Result<Void, OpenHaystackMainView.AlertType>) -> Void) {
AnisetteDataManager.shared.requestAnisetteData { result in
AnisetteDataManager.shared.requestAnisetteData { [weak self] result in
guard let self = self else { return }
switch result {
case .failure(_):
completion(.failure(.activatePlugin))
@@ -173,7 +174,7 @@ class AccessoryController: ObservableObject {
return
}
self.findMyController.fetchReports(for: self.accessories, with: token) { result in
self.findMyController.fetchReports(for: self.accessories, with: token) { [weak self] result in
switch result {
case .failure(let error):
os_log(.error, "Downloading reports failed %@", error.localizedDescription)
@@ -183,7 +184,7 @@ class AccessoryController: ObservableObject {
if reports.isEmpty {
completion(.failure(.noReportsFound))
} else {
self.updateWithDecryptedReports(devices: devices)
self?.updateWithDecryptedReports(devices: devices)
completion(.success(()))
}
}

View File

@@ -28,8 +28,8 @@ class AccessoryNearbyMonitor: BluetoothAccessoryDelegate {
}
func initTimer() {
self.cleanup = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
self.removeNearbyAccessories()
self.cleanup = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] _ in
self?.removeNearbyAccessories()
}
}

View File

@@ -45,7 +45,8 @@ class AccessoryAnnotationView: MKAnnotationView {
func updateView() {
guard let accessory = (self.annotation as? AccessoryAnnotation)?.accessory else { return }
self.pinView?.removeFromSuperview()
self.pinView = NSHostingView(rootView: AccessoryPinView(accessory: accessory))
self.pinView = nil
self.pinView = NSHostingView(rootView: AccessoryPinView(accessory: accessory)) // LEAK! Something swiftui?
self.addSubview(pinView!)

View File

@@ -52,8 +52,8 @@ final class MapViewController: NSViewController, MKMapViewDelegate {
}
func zoomInOn(annotations: [MKAnnotation]) {
DispatchQueue.main.async {
self.mapView.showAnnotations(annotations, animated: true)
DispatchQueue.main.async { [weak self] in
self?.mapView.showAnnotations(annotations, animated: true)
}
}