0

I am using Xamarin in C# to get records saved in CloudKit private storage, in time order. 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. But using the NSSortDescriptor prevents any records to be returned.

-- createdAt

lolelo
  • 698
  • 6
  • 18

1 Answers1

1

As a workaround I added another custom field called time - and set it to 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.

lolelo
  • 698
  • 6
  • 18