我使用以下代码检索记录,更改检索记录中的字段并使用saveRecord将其存储回来:我注意到saveRecord dos没有返回错误,但记录没有在仪表板中更新。
我在saveRecord中看到一个警告:引用它只有在保存的记录比现有记录更新时才有效。如何让它更新?不更新字段不这样做吗?
执行代码时我得到的NSLogs打印在代码之后。
编辑使用CKModifyRecordsOperation保存而不是saveRecord有效。代码如下。所以,我想使用saveRecord,我们需要做更多的事情(更改标签?)以使其有效?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@", @"MacKerricher State Park"];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Artwork" predicate:predicate];
[publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
if (error) {
// Error handling for failed fetch from public database
NSLog(@"Failed to retrieve record due to error %@",error) ;
}
else {
// Modify the record and save it to the database
if([results count])
{
CKRecord *artworkRecord = (CKRecord *)results[0] ;
NSLog(@"Retrieved record %@",artworkRecord) ;
//NSDate *date = ;
artworkRecord[@"date"] = [NSDate date];
NSLog(@"Saving record %@",artworkRecord) ;
[publicDatabase saveRecord:artworkRecord completionHandler:^(CKRecord *savedRecord, NSError *saveError){
if (!error)
{
NSLog(@"Successfully resaved artist record %@",savedRecord) ;
}
else
{
// Insert error handling
NSLog(@"Failed to store record due to error %@",error) ;
}
}];
}
}
}];
** NSLog执行**
时从上面的代码打印 2016-03-13 23:36:50.832 ScreenShare[903:442196] Retrieved record <CKRecord: 0x134e51850; recordType=Artwork, recordID=4FEA41AA-2DE2-49C2-8B6E-513B5D3AB0AD:(_defaultZone:__defaultOwner__), recordChangeTag=ilr95tq6, values={
address = "Fort Bragg, CA";
date = "2016-03-14 00:39:28 +0000";
rating = 5;
title = "MacKerricher State Park";
}>
2016-03-13 23:36:50.834 ScreenShare[903:442196] Saving record <CKRecord: 0x134e51850; recordType=Artwork, recordID=4FEA41AA-2DE2-49C2-8B6E-513B5D3AB0AD:(_defaultZone:__defaultOwner__), recordChangeTag=ilr95tq6, values={
address = "Fort Bragg, CA";
date = "2016-03-14 06:36:50 +0000";
rating = 5;
title = "MacKerricher State Park";
}>
2016-03-13 23:36:51.292 ScreenShare[903:442197] Successfully resaved artist record (null)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title = %@", @"MacKerricher State Park"];
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Artwork" predicate:predicate];
[publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray *results, NSError *error) {
if (error) {
// Error handling for failed fetch from public database
NSLog(@"Failed to retrieve record due to error %@",error) ;
}
else {
// Modify the record and save it to the database
if([results count])
{
CKRecord *artworkRecord = (CKRecord *)results[0] ;
NSLog(@"Retrieved record %@",artworkRecord) ;
//NSDate *date = ;
artworkRecord[@"date"] = [NSDate date];
// Using saveRecord: method of database does not work
// NSLog(@"Saving record %@",artworkRecord) ;
// [publicDatabase saveRecord:artworkRecord completionHandler:^(CKRecord *savedRecord, NSError *saveError){
// if (!error)
// {
// NSLog(@"Successfully resaved artist record %@",savedRecord) ;
// }
// else
// {
// // Insert error handling
// NSLog(@"Failed to store record due to error %@",error) ;
// }
// }];
CKModifyRecordsOperation *saveOperation = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:@[artworkRecord] recordIDsToDelete:nil] ;
saveOperation.modifyRecordsCompletionBlock = ^(NSArray *savedRecords, NSArray *deletedRecordIDs, NSError * operationError)
{
if(operationError)
{
NSLog(@"Save failed with error %@",operationError) ;
}
} ;
[publicDatabase addOperation:saveOperation] ;
}
}
}];
答案 0 :(得分:1)
以下是更改选项的保存。
func notifyCollectionComplete(theCollection: Int) {
let container = CKContainer(identifier: "iCloud.blah")
let publicDB = container.publicCloudDatabase
let singleLink2LinkthemALL = CKRecordID(recordName: uniqReference)
let newRecord = CKRecord(recordType: "Collections", recordID: singleLink2LinkthemALL)
newRecord.setObject(theCollection, forKey: "theCount")
var localChanges:[CKRecord] = []
localChanges.append(newRecord)
let saveRecordsOperation = CKModifyRecordsOperation(recordsToSave: localChanges, recordIDsToDelete: nil)
saveRecordsOperation.savePolicy = .ChangedKeys
saveRecordsOperation.perRecordCompletionBlock = { record, error in
if error != nil {
self.showAlert(message: error!.localizedDescription)
print(error!.localizedDescription)
}
// deal with conflicts
// set completionHandler of wrapper operation if it's the case
}
saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error != nil {
self.showAlert(message: error!.localizedDescription)
print(error!.localizedDescription)
} else {
// deal with conflictsay
// set completionHandler of wrapper operation if it's the case
}
}
publicDB.addOperation(saveRecordsOperation)
}