Search code examples
swiftdateicloudpredicate

Fetching iCloudKit records older than 24 hours


I am sorry have a stupid easy question. I want to list all records created that are older than 24 hours. The query field for the creation date is set on the dashboard. I have this code... but I cannot seem to get my head around the problem.

I got this code ...

    let container = CKContainer(identifier: "iCloud.blah")
    let publicDB = container.publicCloudDatabase
// 24 hours ago
    let date = NSDate(timeInterval: -60.0 * 1440, sinceDate: NSDate())
    let predicate = NSPredicate(format: "creationDate < %@", date)
    let query = CKQuery(recordType: "Collection", predicate: predicate)

But this gets me all the records that have been created in the last 24 hours; I don't want them, I want all the records that are older than 24 hours. My record contains only one field, and that is an asset. Starting to think that is my problem in part...

enter image description here

The index size if deployed doesn't look promising does it? 0 bytes?


Solution

  • Ok, I think I found the answer to my problem. This code is good; my mistake was in selecting only the recordIDs when I was choosing records. I needed to select the meta data too.

    readerOperation = CKQueryOperation(query: query) readerOperation.desiredKeys = ["record.recordID.recordName","record.creationDate"];

        readerOperation.recordFetchedBlock = { (record) in
            self.records2Erase.append(record.recordID)
        }
        readerOperation.queryCompletionBlock = {(cursor, error) in
                if error != nil {
                    dispatch_async(dispatch_get_main_queue()) {
                        self.showAlert(message: error!.localizedDescription)
                    }
                } else {                 
                    print("records2Erase since \(date) \(self.records2Erase)")
                }
    
    
        }