Проблемы с заменой записей CloudKit

Я получаю фатальную ошибку: "Не удалось передать значение типа" NSTaggedPointerString "в" NSData "в строке" if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data) as? CKRecord) != nil ".

Возникла еще одна фатальная ошибка: "Попытка установить объект не-свойство-списка как значение NSUserDefaults/CFPreferences для ключевого locationData", когда я пытаюсь сохранить locationRecord как значение по умолчанию.

var locationRecord = CKRecord(recordType: "location")

func getRecordToUpdate(_ locations:CLLocation)
{
    if defaults1.object(forKey: "locationData") == nil{
        locationRecord.setObject(locations, forKey: "location")
        defaults1.set(locationRecord, forKey: "locationData")
        self.updateLocationRecord(locations: locations)
    }else{
        if let loadedData = defaults1.object(forKey: "locationData") {
            print("continue")
            if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data)) != nil
            {
                let publicDB = CKContainer.default().publicCloudDatabase
                publicDB.fetch(withRecordID: locationRecord.recordID,completionHandler: {
                    (record, error) in
                    if error == nil
                    {
                        publicDB.delete(withRecordID: (record?.recordID)!, completionHandler: {
                            (record, error) in
                            if(error == nil){
                                print("old record delete")
                                self.locationRecord.setObject(locations, forKey: "location")
                                self.defaults1.set(self.locationRecord, forKey: "locationData")
                                self.updateLocationRecord(locations: locations)
                            }
                            else{
                            }
                        })
                    }else{
                        print("Error fetching previous record")
                        }
                })
            }
        }
    }
}
    func updateLocationRecord(locations: CLLocation)
    {
        locationRecord.setObject(locations, forKey: "location")
        let publicData = CKContainer.default().publicCloudDatabase
        publicData.save(locationRecord, completionHandler: { record, error in
        })
        if error == nil
        {
            print("Location saved")
        }
    }

Ответ 1

Вот код, который закончился для меня:

if let loadedData = defaults1.object(forKey: "locationData") as? Data {
                let litt = NSKeyedUnarchiver.unarchiveObject(with: loadedData) as! CKRecord
                let publicDB = CKContainer.default().publicCloudDatabase
                    publicDB.fetch(withRecordID: litt.recordID ,completionHandler: {
                        (record, error) in
                        if error == nil
                        {
                            publicDB.delete(withRecordID: (record?.recordID)!, completionHandler: {
                                (record, error) in
                                if(error == nil){
                                    print("old record delete")
                                    let id = self.locationRecord.recordID.recordName
                                    self.locationRecord.setObject(locations, forKey: "location")
                                    self.defaults1.set(id, forKey: "locationData")
                                    self.updateLocationRecord(locations: locations)
                                }
                                else{
                                }
                            })
                        }else{
                            print("Error fetching previous record")
                            }
                    })
                }
        }
    }