我正在使用以下代码来检索记录,更改检索到的记录中的字段,然后使用saveRecord将其存储回去。我注意到saveRecord不会返回错误,但记录不会在仪表板中更新。

我在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] ;
        }
    }
}];

最佳答案

这是保存的选项更改。

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)


}

10-08 15:49