我需要获取 cloudkit 中的最后一条记录。这是我的代码:
CKContainer *container = [CKContainer containerWithIdentifier:containerID];
CKDatabase *publicDatabase = [container publicCloudDatabase];
CKQuery *query = [[CKQuery alloc] initWithRecordType:recordType
predicate:[NSPredicate predicateWithFormat:@"TRUEPREDICATE"]];
CKQueryOperation *queryOp = [[CKQueryOperation alloc] initWithQuery:query];
queryOp.desiredKeys = @[@"record.recordID.recordName"];
queryOp.recordFetchedBlock = ^(CKRecord *record)
{
//do something
};
queryOp.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *error)
{
NSLog(@"CKQueryCursor error %@", error);
};
queryOp.resultsLimit = CKQueryOperationMaximumResults;
[publicDatabase addOperation:queryOp];
我的问题是如何修改我的代码以获取 cloudkit 中的最后一条记录?
我会非常感谢你的帮助
最佳答案
您可以对创建数据进行升序排序,然后像这样要求 1 个结果(代码在 Swift 中):
添加排序:
query.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
限制结果:
queryOp.resultsLimit = 1
关于iphone - CKQueryOperation 获取最后一条记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27235774/