我有一个包含2种记录类型的云工具包数据库:
人名单
房间清单
占用房间的人不断变化。我有一个CKRecord
.Reference存储在引用一个房间的人员记录类型中。但是,此参考需要不断变化,因为占用房间的人不断变化。
我一直在努力尝试在不创建新记录的情况下更新参考。
我试图将CKRecord
从人员的CkRecords
数组中拉出,创建对我希望该人员居住的房间的新引用,并将此引用保存回人员记录,然后使用CKModifyRecordsOperation
对象。
但是,这似乎在我的数据库中创建了一条新记录,而不是更新当前记录。
if providerAssignedRoom == nil {
self.roomAssignmentDelegate?.assignRoom(roomID: self.selectedRoomCKRecID, provider: provider, completion: self.transitionBackToRoot)
}
else {
let newProvider = self.searchCKRecArrayForProviderRef(providerRecID, self.providersCKRecsArray)
let reference = CKRecord.Reference(recordID:
self.selectedRoomCKRecID, action: .none)
newProvider[RecordTypes.providerEnum.assignedRoom] = reference
let operation = CKModifyRecordsOperation(recordsToSave: [record, newProvider], recordIDsToDelete: nil)
operation.completionBlock = {
print("record reference assigned to nil")
}
LaborFloorNamesDB.share.publicDatabase.add(operation)
}
最佳答案
解决方案只是添加一个保存策略。我使它变得比需要的复杂。
let providerToBeAssigned = self.providersArray[indexPath.item]
let providerToBeAssignedCKRef = self.findProviderCKRecordBasedOnProviderObject(provider: providerToBeAssigned)
var roomToBeReferenced: CKRecord {
let object = self.roomsAsCKRecsArray.filter {$0.recordID == self.selectedRoomCKRecID}
let value = object.first
return value!
}
if self.providerToBeRemoved != nil {
self.providerToBeRemoved![RecordTypes.providerEnum.assignedRoom] = nil
}
providerToBeAssignedCKRef![RecordTypes.providerEnum.assignedRoom] = CKRecord.Reference(record: roomToBeReferenced, action: .none)
let operation = CKModifyRecordsOperation(recordsToSave: [providerToBeAssignedCKRef!, self.providerToBeRemoved!], recordIDsToDelete: nil)
operation.savePolicy = .changedKeys
operation.completionBlock = {
print("record reference updated to cloud")
}
LaborFloorNamesDB.share.publicDatabase.add(operation)