Skip to content

Commit f878443

Browse files
committed
Core Data: run functions in context closure
1 parent 143b7c7 commit f878443

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

InfiniLink/BLE/BLEManager.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,10 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
187187
}
188188

189189
func removeDevice(device: Device? = nil) {
190-
deviceManager.removeDevice(device ?? pairedDevice)
191-
unpair(device: device)
190+
Task {
191+
await deviceManager.removeDevice(device ?? pairedDevice)
192+
unpair(device: device)
193+
}
192194
}
193195

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

199201
func unpair(device: Device? = nil) {
200-
if let pairedDevice {
201-
// Delete the device object we have said for this watch
202-
deviceManager.removeDevice(device ?? pairedDevice)
202+
Task {
203+
if let pairedDevice {
204+
// Delete the device object we have said for this watch
205+
await deviceManager.removeDevice(device ?? pairedDevice)
206+
}
207+
// Update the list of user watches
208+
await deviceManager.fetchAllDevices()
203209
}
204-
// Update the list of user watches
205-
deviceManager.fetchAllDevices()
206210

207211
if let first = deviceManager.watches.first, deviceManager.watches.count <= 1 {
208212
// Switch to the user's next watch

InfiniLink/BLE/DeviceManager.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,8 @@ class DeviceManager: ObservableObject {
144144
infineatWatchFace.showSideCover = settings.watchFaceInfineat.showSideCover
145145
device.watchFaceInfineat = infineatWatchFace
146146

147-
do {
148-
try context.save()
149-
} catch {
150-
log(error.localizedDescription, caller: "DeviceManager - updateSettings")
147+
Task {
148+
await save()
151149
}
152150

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

160158
device.name = name
161159

162-
do {
163-
try context.save()
164-
} catch {
165-
log(error.localizedDescription, caller: "DeviceManager - updateName")
160+
Task {
161+
await save()
166162
}
167163
}
168164

@@ -183,24 +179,38 @@ class DeviceManager: ObservableObject {
183179
}
184180
}
185181

186-
func removeDevice(_ device: Device) {
182+
func removeDevice(_ device: Device) async {
187183
do {
188-
context.delete(device)
189-
try context.save()
184+
try await context.perform {
185+
self.context.delete(device)
186+
try self.context.save()
187+
}
190188
} catch {
191189
log(error.localizedDescription, caller: "DeviceManager - removeDevice")
192190
}
193191
}
194192

195-
func fetchAllDevices() {
193+
func fetchAllDevices() async {
196194
let fetchRequest: NSFetchRequest<Device> = Device.fetchRequest()
197195

198196
do {
199-
self.watches = try context.fetch(fetchRequest)
197+
try await context.perform {
198+
self.watches = try self.context.fetch(fetchRequest)
199+
}
200200
} catch {
201201
log("Error fetching devices: \(error.localizedDescription)", caller: "DeviceManager")
202202
}
203203
}
204+
205+
func save() async {
206+
do {
207+
try await context.perform {
208+
try self.context.save()
209+
}
210+
} catch {
211+
log(error.localizedDescription, caller: "DeviceManager - updateSettings")
212+
}
213+
}
204214
}
205215

206216
extension DeviceManager {

InfiniLink/Core/Connect/MyDevicesView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ struct MyDevicesView: View {
9797
ConnectView()
9898
}
9999
.onAppear {
100-
deviceManager.fetchAllDevices()
100+
Task {
101+
await deviceManager.fetchAllDevices()
102+
}
101103
}
102104
}
103105
.navigationViewStyle(.stack)

0 commit comments

Comments
 (0)