Swiftdata get-error(线程 6:EXC_BREAKPOINT(代码 = 1,子代码 = 0x1cc165d0c)) - 插入新的 swiftdata 时出现问题

问题描述 投票:0回答:1

当我执行经典的 modelcontext.insert(myModel) 时,一切正常。但由于我想同时使用 myModels UUID 作为标识符初始化本地通知,因此应用程序崩溃了。现在,我尝试不直接初始化通知,只是在查看新插入的 myModel 时读取 UUID.uuidstring。但它无法获取 UUID(在我的例子中该变量称为 objectID),它只会崩溃。为什么?

错误是:

线程 6:EXC_BREAKPOINT(代码=1,子代码=0x1cc165d0c

我的型号:

@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        
    }
}

以及保存模型的代码:

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()        
}

当应用程序崩溃时,这就是我在 UUID 变量下得到的:

{
@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)
    }
}

}

swift swift-data
1个回答
0
投票

像您的

newObject
这样的模型对象无法传递到
Task
闭包,因为它不可发送,它不符合
Sendable
协议,因此您需要传递 UUID 或字符串。

let uuidString = newObject.objectID.uuidString
Task {
    var localNotification = LocalNotification(identifier: uuidString,
                                              title: "Time to check",
                                              body: "\(objectType) in \(objectPlacement)",
                                              timeInterval: timeintervalMonth,
                                              repeats: false)
    //...
    // don't forget to change localNotification.userInfoMoode
}
© www.soinside.com 2019 - 2024. All rights reserved.