CKQueryOperation returning no results when sort descriptors are used

220 views Asked by At

I am working on a CloudKit based project where it would be very helpful to use sort descriptors to get the most recent results from the database.

func getConversationPosts(for targetConversation: MessageConversation, completionHandler: @escaping ([MessagePost]) -> Void) {
       var post = MessagePost()
       let getRecordsOperation = CKQueryOperation()
        getRecordsOperation.qualityOfService = .userInteractive
        getRecordsOperation.resultsLimit = 1
        getRecordsOperation.query = CKQuery(recordType: "MessagePost", predicate: NSPredicate(format: "conversationName = %@", targetConversation.conversationID.recordName))
        getRecordsOperation.query?.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] // If you comment this out results are returned
        /*getPostsOperation.queryCompletionBlock = {(searchCursor, error) in
            print("sQ: \(searchCursor)")
        }*/
        getRecordsOperation.recordFetchedBlock = {(postRecord) in
            let newPost = MessagePost()
            newPost.textContent = postRecord.value(forKey: "textContent") as! String
            newPost.poster = User(userName: postRecord.value(forKey: "postUser") as! String)
           // posts.append(newPost)
            post = newPost
        }
        getRecordsOperation.completionBlock = {() in
            completionHandler(post)
        }
        OperationQueue().addOperation(getRecordsOperation)
}

The above code always works if the line that adds the sortDescriptors is removed. Even without a result limit, if any sort descriptor is added, the recordFetchedBlock isn't even called, but the completion block is. What could be causing this?

0

There are 0 answers