When I do the classic modelcontext.insert(myModel) everything works fine. But since I want at the same time initialize a local notification using myModels UUID as the identifier the app crashes. Now, I tried out not initializing the notification directly, just reading the UUID.uuidstring when viewing the newly inserted myModel. But it cannot fetch the UUID (the variable is called objectID in my case), it only crashes. Why?
The error is: Thread 6: EXC_BREAKPOINT (code=1, subcode=0x1cc165d0c
My model:
final class HomeObject: ObservableObject {
let objectID: UUID = UUID()
var objectType: ObjectType?
var roomPictogram: RoomPictograms?
var dateOfPurchase: Date?
var checkTimeInterval: Int?
var replacementDate: Date?
init(objectType: ObjectType?, room: String?, roomPictogram: RoomPictograms?, dateOfPurchase: Date? = nil, checkTimeInterval: Int?, lastChecked: Date?, checkDate: Date?, replacementDate: Date?, home: Home?) {
self.objectType = objectType
self.roomPictogram = roomPictogram
self.dateOfPurchase = dateOfPurchase
self.checkTimeInterval = checkTimeInterval
self.lastChecked = lastChecked
self.checkDate = checkDate
self.replacementDate = replacementDate
<code>@Model
final class HomeObject: ObservableObject {
let objectID: UUID = UUID()
var objectType: ObjectType?
var room: String?
var roomPictogram: RoomPictograms?
var dateOfPurchase: Date?
var checkTimeInterval: Int?
var lastChecked: Date?
var checkDate: Date?
var replacementDate: Date?
var home: Home?
init(objectType: ObjectType?, room: String?, roomPictogram: RoomPictograms?, dateOfPurchase: Date? = nil, checkTimeInterval: Int?, lastChecked: Date?, checkDate: Date?, replacementDate: Date?, home: Home?) {
self.objectType = objectType
self.room = room
self.roomPictogram = roomPictogram
self.dateOfPurchase = dateOfPurchase
self.checkTimeInterval = checkTimeInterval
self.lastChecked = lastChecked
self.checkDate = checkDate
self.replacementDate = replacementDate
self.home = home
}
}
</code>
@Model
final class HomeObject: ObservableObject {
let objectID: UUID = UUID()
var objectType: ObjectType?
var room: String?
var roomPictogram: RoomPictograms?
var dateOfPurchase: Date?
var checkTimeInterval: Int?
var lastChecked: Date?
var checkDate: Date?
var replacementDate: Date?
var home: Home?
init(objectType: ObjectType?, room: String?, roomPictogram: RoomPictograms?, dateOfPurchase: Date? = nil, checkTimeInterval: Int?, lastChecked: Date?, checkDate: Date?, replacementDate: Date?, home: Home?) {
self.objectType = objectType
self.room = room
self.roomPictogram = roomPictogram
self.dateOfPurchase = dateOfPurchase
self.checkTimeInterval = checkTimeInterval
self.lastChecked = lastChecked
self.checkDate = checkDate
self.replacementDate = replacementDate
self.home = home
}
}
And the code for saving the model:
<code> func addItem(_ homes: [Home], _ modelContext: ModelContext, _ dismiss: DismissAction) {
var replacementDate: Date = .now
if dateOfPurchaseBool == false {
replacementDate = .now.addingTimeInterval(findReplaceTimeInterval(objectType))
replacementDate = dateOfPurchase.addingTimeInterval(findReplaceTimeInterval(objectType))
let checkTimeInterval = findTimeInterval(objectType)
var components = DateComponents()
components.month = checkTimeInterval
return Calendar.current.date(byAdding: components, to: .now)!
let newObject = HomeObject(objectType: objectType, room: objectPlacement, roomPictogram: roomImage, dateOfPurchase: dateOfPurchase, checkTimeInterval: checkTimeInterval, lastChecked: .now, checkDate: checkDate, replacementDate: replacementDate, home: findHome(HomeViewModel().selectedHome!, homes))
modelContext.insert(newObject)
modelContext.insert(RoomModel(rooms: objectPlacement))
let timeintervalMonth = checkDate.timeIntervalSinceNow + 100
var localNotification = LocalNotification(identifier: newObject.objectID.uuidString,
body: "(objectType) in (objectPlacement)",
timeInterval: timeintervalMonth,
localNotification.subtitle = ""
localNotification.bundleImageName = ""
localNotification.userInfo = ["nextView" : NextView.homeObjectDetailView.rawValue]
localNotification.userInfoMoode = ["object" : newObject.objectID.uuidString]
localNotification.categoryIdentifier = "snooze"
await NotificationViewModel().schedule(localNotification: localNotification, newObject)
<code> func addItem(_ homes: [Home], _ modelContext: ModelContext, _ dismiss: DismissAction) {
var replacementDate: Date = .now
if dateOfPurchaseBool == false {
dateOfPurchase = .now
replacementDate = .now.addingTimeInterval(findReplaceTimeInterval(objectType))
} else {
replacementDate = dateOfPurchase.addingTimeInterval(findReplaceTimeInterval(objectType))
}
let checkTimeInterval = findTimeInterval(objectType)
var checkDate: Date {
var components = DateComponents()
components.month = checkTimeInterval
return Calendar.current.date(byAdding: components, to: .now)!
}
let newObject = HomeObject(objectType: objectType, room: objectPlacement, roomPictogram: roomImage, dateOfPurchase: dateOfPurchase, checkTimeInterval: checkTimeInterval, lastChecked: .now, checkDate: checkDate, replacementDate: replacementDate, home: findHome(HomeViewModel().selectedHome!, homes))
modelContext.insert(newObject)
modelContext.insert(RoomModel(rooms: objectPlacement))
let timeintervalMonth = checkDate.timeIntervalSinceNow + 100
Task {
var localNotification = LocalNotification(identifier: newObject.objectID.uuidString,
title: "Time to check",
body: "(objectType) in (objectPlacement)",
timeInterval: timeintervalMonth,
repeats: false)
localNotification.subtitle = ""
localNotification.bundleImageName = ""
localNotification.userInfo = ["nextView" : NextView.homeObjectDetailView.rawValue]
localNotification.userInfoMoode = ["object" : newObject.objectID.uuidString]
localNotification.categoryIdentifier = "snooze"
await NotificationViewModel().schedule(localNotification: localNotification, newObject)
}
dismiss()
}
</code>
func addItem(_ homes: [Home], _ modelContext: ModelContext, _ dismiss: DismissAction) {
var replacementDate: Date = .now
if dateOfPurchaseBool == false {
dateOfPurchase = .now
replacementDate = .now.addingTimeInterval(findReplaceTimeInterval(objectType))
} else {
replacementDate = dateOfPurchase.addingTimeInterval(findReplaceTimeInterval(objectType))
}
let checkTimeInterval = findTimeInterval(objectType)
var checkDate: Date {
var components = DateComponents()
components.month = checkTimeInterval
return Calendar.current.date(byAdding: components, to: .now)!
}
let newObject = HomeObject(objectType: objectType, room: objectPlacement, roomPictogram: roomImage, dateOfPurchase: dateOfPurchase, checkTimeInterval: checkTimeInterval, lastChecked: .now, checkDate: checkDate, replacementDate: replacementDate, home: findHome(HomeViewModel().selectedHome!, homes))
modelContext.insert(newObject)
modelContext.insert(RoomModel(rooms: objectPlacement))
let timeintervalMonth = checkDate.timeIntervalSinceNow + 100
Task {
var localNotification = LocalNotification(identifier: newObject.objectID.uuidString,
title: "Time to check",
body: "(objectType) in (objectPlacement)",
timeInterval: timeintervalMonth,
repeats: false)
localNotification.subtitle = ""
localNotification.bundleImageName = ""
localNotification.userInfo = ["nextView" : NextView.homeObjectDetailView.rawValue]
localNotification.userInfoMoode = ["object" : newObject.objectID.uuidString]
localNotification.categoryIdentifier = "snooze"
await NotificationViewModel().schedule(localNotification: localNotification, newObject)
}
dismiss()
}
When the app is crashing this is what i get underneath the UUID variable:
@storageRestrictions(accesses: _$backingData, initializes: _objectID)
_$backingData.setValue(forKey: .objectID, to: initialValue)
_objectID = _SwiftDataNoType()
_$observationRegistrar.access(self, keyPath: .objectID)
return self.getValue(forKey: .objectID)
_$observationRegistrar.withMutation(of: self, keyPath: .objectID) {
self.setValue(forKey: .objectID, to: newValue)
<code>{
@storageRestrictions(accesses: _$backingData, initializes: _objectID)
init(initialValue) {
_$backingData.setValue(forKey: .objectID, to: initialValue)
_objectID = _SwiftDataNoType()
}
get {
_$observationRegistrar.access(self, keyPath: .objectID)
return self.getValue(forKey: .objectID)
}
set {
_$observationRegistrar.withMutation(of: self, keyPath: .objectID) {
self.setValue(forKey: .objectID, to: newValue)
}
}
</code>
{
@storageRestrictions(accesses: _$backingData, initializes: _objectID)
init(initialValue) {
_$backingData.setValue(forKey: .objectID, to: initialValue)
_objectID = _SwiftDataNoType()
}
get {
_$observationRegistrar.access(self, keyPath: .objectID)
return self.getValue(forKey: .objectID)
}
set {
_$observationRegistrar.withMutation(of: self, keyPath: .objectID) {
self.setValue(forKey: .objectID, to: newValue)
}
}
}