Skip to content

Commit 0326163

Browse files
committed
bugfix: fix more crashes
1 parent c25a727 commit 0326163

3 files changed

Lines changed: 45 additions & 31 deletions

File tree

InfiniLink/BLE/BLEWriteManager.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ struct BLEWriteManager {
154154
}
155155
}
156156

157-
// MARK: Helper functions
158157
extension BLEWriteManager {
159158
func timeSince1970() -> [UInt8] {
160159
let timeInterval : UInt64 = UInt64(Date().timeIntervalSince1970)

InfiniLink/BLE/DeviceManager.swift

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,31 +130,40 @@ class DeviceManager: ObservableObject {
130130
// Get settings from settings file from watch and save it to keep device object up-to-date
131131
func updateSettings(settings: Settings) {
132132
guard let device = fetchDevice() else { return }
133+
let context = persistenceController.container.viewContext
133134

134-
device.brightLevel = Int16(settings.brightLevel.rawValue)
135-
device.chimesOption = Int16(settings.chimesOption.rawValue)
136-
device.clockType = Int16(settings.clockType.rawValue)
137-
device.notificationStatus = Int16(settings.notificationStatus.rawValue)
138-
device.shakeWakeThreshold = Int16(settings.watchFace)
139-
device.watchface = Int16(settings.watchFace)
140-
device.weatherFormat = Int16(settings.weatherFormat.rawValue)
141-
device.stepsGoal = Int32(settings.stepsGoal)
142-
device.screenTimeout = Int32(settings.screenTimeOut)
143-
144-
let pineTimeStyle = PineTimeStyleWatchface(context: persistenceController.container.viewContext)
145-
pineTimeStyle.colorBG = Int16(settings.pineTimeStyle.ColorBG.rawValue)
146-
pineTimeStyle.colorBar = Int16(settings.pineTimeStyle.ColorBar.rawValue)
147-
pineTimeStyle.colorTime = Int16(settings.pineTimeStyle.ColorTime.rawValue)
148-
pineTimeStyle.guageStyle = Int16(settings.pineTimeStyle.gaugeStyle.rawValue)
149-
pineTimeStyle.weatherEnable = Int16(settings.pineTimeStyle.weatherEnable.rawValue)
150-
device.pineTimeStyle = pineTimeStyle
151-
152-
let infineatWatchFace = InfineatWatchface(context: persistenceController.container.viewContext)
153-
infineatWatchFace.colorIndex = Int16(settings.watchFaceInfineat.colorIndex)
154-
infineatWatchFace.showSideCover = settings.watchFaceInfineat.showSideCover
155-
device.watchFaceInfineat = infineatWatchFace
156-
157-
persistenceController.save()
135+
// MARK: - Crash
136+
// Crashes happening when creating watch face objects from the viewContext
137+
context.perform {
138+
device.brightLevel = Int16(settings.brightLevel.rawValue)
139+
device.chimesOption = Int16(settings.chimesOption.rawValue)
140+
device.clockType = Int16(settings.clockType.rawValue)
141+
device.notificationStatus = Int16(settings.notificationStatus.rawValue)
142+
device.shakeWakeThreshold = Int16(settings.watchFace)
143+
device.watchface = Int16(settings.watchFace)
144+
device.weatherFormat = Int16(settings.weatherFormat.rawValue)
145+
device.stepsGoal = Int32(settings.stepsGoal)
146+
device.screenTimeout = Int32(settings.screenTimeOut)
147+
148+
let pineTimeStyle = PineTimeStyleWatchface(context: context)
149+
pineTimeStyle.colorBG = Int16(settings.pineTimeStyle.ColorBG.rawValue)
150+
pineTimeStyle.colorBar = Int16(settings.pineTimeStyle.ColorBar.rawValue)
151+
pineTimeStyle.colorTime = Int16(settings.pineTimeStyle.ColorTime.rawValue)
152+
pineTimeStyle.guageStyle = Int16(settings.pineTimeStyle.gaugeStyle.rawValue)
153+
pineTimeStyle.weatherEnable = Int16(settings.pineTimeStyle.weatherEnable.rawValue)
154+
device.pineTimeStyle = pineTimeStyle
155+
156+
let infineatWatchFace = InfineatWatchface(context: context)
157+
infineatWatchFace.colorIndex = Int16(settings.watchFaceInfineat.colorIndex)
158+
infineatWatchFace.showSideCover = settings.watchFaceInfineat.showSideCover
159+
device.watchFaceInfineat = infineatWatchFace
160+
161+
do {
162+
try context.save()
163+
} catch {
164+
log("Error saving settings: \(error.localizedDescription)", caller: "DeviceManager - updateSettings")
165+
}
166+
}
158167
getSettings()
159168
}
160169

@@ -185,13 +194,19 @@ class DeviceManager: ObservableObject {
185194
}
186195

187196
func removeDevice(_ device: Device) {
197+
let objectID = device.objectID
188198
let context = persistenceController.container.newBackgroundContext()
189199

190-
do {
191-
context.delete(device)
192-
try context.save()
193-
} catch {
194-
log("Error removing device: \(error.localizedDescription)", caller: "DeviceManager")
200+
context.perform {
201+
do {
202+
if let deviceToDelete = context.object(with: objectID) as? Device {
203+
context.delete(deviceToDelete)
204+
try context.save()
205+
log("Successfully removed device", caller: "DeviceManager")
206+
}
207+
} catch {
208+
log("Error removing device: \(error.localizedDescription)", caller: "DeviceManager")
209+
}
195210
}
196211
}
197212

InfiniLink/Core/DeviceView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct DeviceView: View {
222222
.onChange(of: bleManager.blefsTransfer) { blefsTransfer in
223223
if blefsTransfer != nil && scenePhase == .active {
224224
BLEFSHandler.shared.readSettings { settings in
225-
// MARK: - Candidate
225+
// MARK: - Crash
226226
// This is getting called while in the background, and fetching/updating objects from the UI/main thread, and could be the source of crashes
227227
// We need to add a check to make sure we're in the foreground when we call this method
228228
deviceManager.updateSettings(settings: settings)

0 commit comments

Comments
 (0)