@@ -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
0 commit comments