Skip to content

Commit

Permalink
Core Data: run functions in context closure
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcharger committed Feb 2, 2025
1 parent 143b7c7 commit f878443
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
18 changes: 11 additions & 7 deletions InfiniLink/BLE/BLEManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
}

func removeDevice(device: Device? = nil) {
deviceManager.removeDevice(device ?? pairedDevice)
unpair(device: device)
Task {
await deviceManager.removeDevice(device ?? pairedDevice)
unpair(device: device)
}
}

func resetDevice() {
Expand All @@ -197,12 +199,14 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
}

func unpair(device: Device? = nil) {
if let pairedDevice {
// Delete the device object we have said for this watch
deviceManager.removeDevice(device ?? pairedDevice)
Task {
if let pairedDevice {
// Delete the device object we have said for this watch
await deviceManager.removeDevice(device ?? pairedDevice)
}
// Update the list of user watches
await deviceManager.fetchAllDevices()
}
// Update the list of user watches
deviceManager.fetchAllDevices()

if let first = deviceManager.watches.first, deviceManager.watches.count <= 1 {
// Switch to the user's next watch
Expand Down
36 changes: 23 additions & 13 deletions InfiniLink/BLE/DeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,8 @@ class DeviceManager: ObservableObject {
infineatWatchFace.showSideCover = settings.watchFaceInfineat.showSideCover
device.watchFaceInfineat = infineatWatchFace

do {
try context.save()
} catch {
log(error.localizedDescription, caller: "DeviceManager - updateSettings")
Task {
await save()
}

getSettings()
Expand All @@ -159,10 +157,8 @@ class DeviceManager: ObservableObject {

device.name = name

do {
try context.save()
} catch {
log(error.localizedDescription, caller: "DeviceManager - updateName")
Task {
await save()
}
}

Expand All @@ -183,24 +179,38 @@ class DeviceManager: ObservableObject {
}
}

func removeDevice(_ device: Device) {
func removeDevice(_ device: Device) async {
do {
context.delete(device)
try context.save()
try await context.perform {
self.context.delete(device)
try self.context.save()
}
} catch {
log(error.localizedDescription, caller: "DeviceManager - removeDevice")
}
}

func fetchAllDevices() {
func fetchAllDevices() async {
let fetchRequest: NSFetchRequest<Device> = Device.fetchRequest()

do {
self.watches = try context.fetch(fetchRequest)
try await context.perform {
self.watches = try self.context.fetch(fetchRequest)
}
} catch {
log("Error fetching devices: \(error.localizedDescription)", caller: "DeviceManager")
}
}

func save() async {
do {
try await context.perform {
try self.context.save()
}
} catch {
log(error.localizedDescription, caller: "DeviceManager - updateSettings")
}
}
}

extension DeviceManager {
Expand Down
4 changes: 3 additions & 1 deletion InfiniLink/Core/Connect/MyDevicesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ struct MyDevicesView: View {
ConnectView()
}
.onAppear {
deviceManager.fetchAllDevices()
Task {
await deviceManager.fetchAllDevices()
}
}
}
.navigationViewStyle(.stack)
Expand Down

0 comments on commit f878443

Please sign in to comment.