[英]Get saved records from CloudKit in time order
I am using Xamarin in C# to get records saved in CloudKit private storage, in time order.我在 C# 中使用 Xamarin 按时间顺序获取保存在 CloudKit 私有存储中的记录。 It's possible for me to get records without sorting:我可以在不排序的情况下获取记录:
var predicate = NSPredicate.FromValue(true);
var query = new CKQuery(nameof(EventName), predicate); // all records
/*
var sort = new NSSortDescriptor("createdAt", false); // false or true don't matter. no records found
query.SortDescriptors = new NSSortDescriptor[] { sort }; // no result
*/
CKRecord[] records = await Container.PrivateCloudDatabase.PerformQueryAsync(query, null);
I have set the createdAt
field in the CloudKit Dashboard in the developer portal to Queryable
, as mentioned in this thread: CloudKit: Order query results by creation date .我已将开发人员门户中 CloudKit Dashboard 中的createdAt
字段设置为Queryable
,如该线程中所述: CloudKit: Order query results by creation date 。 But using the NSSortDescriptor
prevents any records to be returned.但是使用NSSortDescriptor
阻止返回任何记录。
As a workaround I added another custom field called time
- and set it to MillisecondsUtcNow
:作为一种解决方法,我添加了另一个名为time
的自定义字段 - 并将其设置为MillisecondsUtcNow
:
// https://stackoverflow.com/questions/5955883/datetimes-representation-in-milliseconds
// picking utc now: https://stackoverflow.com/questions/62151/datetime-now-vs-datetime-utcnow
long MillisecondsUtcNow()
{
return (long)(System.DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
}
Then I could successfully get the:然后我可以成功获得:
var sort = new NSSortDescriptor("time", false); // most recent event appear at index 0
query.SortDescriptors = new NSSortDescriptor[] { sort };
CKRecord[] records = await Container.PrivateCloudDatabase.PerformQueryAsync(query, null);
But it would still be nice if someone could answer on how to query the createdAt
System Field.但是,如果有人可以回答如何查询createdAt
系统字段,那仍然会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.