Post

Replies

Boosts

Views

Activity

Why does this function not sort the data?
func getStates(completionQueue: DispatchQueue = .main, completionHandler: @escaping (Result<[StateList], Error>) -> Void) { var fetchedStates: [StateList] = [] let predicate = NSPredicate(value: true) let sort = NSSortDescriptor(key: "name", ascending: true) let query = CKQuery(recordType: "StateList", predicate: predicate) let queryOperation = CKQueryOperation(query: query) query.sortDescriptors = [sort] queryOperation.recordMatchedBlock = { (recordID, recordResult) in if let record = try? recordResult.get() as CKRecord { var stateList = StateList(recordID: record.recordID) stateList.recordID = record.recordID stateList.id = record["id"] as? String ?? "" stateList.name = record["name"] as? String ?? "" stateList.initial = record["initial"] as? String ?? "" fetchedStates.append(stateList) print(record.recordID) } print("recordMatchedBlock") } queryOperation.queryResultBlock = { result in DispatchQueue.main.async { switch result { case .success: completionHandler(.success(fetchedStates)) case .failure(let error): completionHandler(.failure(error)) } } } database.add(queryOperation) }
1
0
405
Jan ’22
iOS 15 CloudKit Public DB Fetch not returning records
My code below runs but does not return the values stored in the database. I am not sure that the issue is since I am fairly new to iOS programing I am a little lost on next steps. There isn't much documentation on the recordMatchedBlock. func getLatestPost(completionQueue: DispatchQueue = .main, completionHandler: @escaping (Result<[Post], Error>) -> Void) { var newPosts: [Post] = [] let predicate = NSPredicate(value: true) let sort1 = NSSortDescriptor(key: "date", ascending: false) let query = CKQuery(recordType: "Post", predicate: predicate) let queryOperation = CKQueryOperation(query: query) query.sortDescriptors = [sort1] queryOperation.desiredKeys = ["id", "title", "content", "category", "GroupID", "featured", "photo", "date"] queryOperation.resultsLimit = 500 queryOperation.recordMatchedBlock = { (_, recordResult) in if let record = try? recordResult.get(), var post = Post(record: record) { post.recordID = record.recordID post.id = record["id"] as? String ?? "" post.title = record["title"] as? String ?? "" post.content = record["content"] as? String ?? "" post.GroupID = record["GroupID"] as? String ?? "" post.featured = record["featured"] as? Bool ?? false post.photo = record["photo"] as? CKAsset newPosts.append(post) print("Post Returned") } print("recordMatchedBlock") } queryOperation.queryResultBlock = { result in DispatchQueue.main.async { switch result { case .success: completionHandler(.success(newPosts)) case .failure(let error): completionHandler(.failure(error)) } } } database.add(queryOperation) }
0
0
795
Nov ’21