Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Lee
2,855 PointsCKQueryOperation returning duplicates
When I perform a query operation, despite only having 501 records in the cloudkit dashboard, I get around 1542 results (all duplicates).
func queryForTable() -> Void {
self.arrayOfFoodItems.removeAllObjects()
let container = CKContainer.defaultContainer()
let resultPredicate = NSPredicate(format: "TRUEPREDICATE")
let query = CKQuery(recordType: "FoodItems", predicate: resultPredicate)
let queryOp = CKQueryOperation(query: query)
let operationQueue = NSOperationQueue()
executeQueryOperation(queryOp, onOperationQueue: operationQueue)
}
func executeQueryOperation(queryOperation: CKQueryOperation, onOperationQueue operationQueue: NSOperationQueue){
queryOperation.database = CKContainer.defaultContainer().publicCloudDatabase
queryOperation.recordFetchedBlock = self.addRecordToArray
queryOperation.queryCompletionBlock = { (cursor: CKQueryCursor?, error: NSError?) -> Void in
if cursor != nil {
if let queryCursor = cursor{
let queryCursorOperation = CKQueryOperation(cursor: queryCursor)
self.executeQueryOperation(queryCursorOperation, onOperationQueue: operationQueue)
}
}
else {
self.sortToSectionsAndReloadData()
}
}
operationQueue.addOperation(queryOperation)
}
func sortToSectionsAndReloadData() {
for (var i = 0; i < self.arrayOfSections.count; i++) {
self.arrayOfArrays[i].removeAllObjects()
let prefix:String = self.arrayOfSections[i]
let array:NSMutableArray = self.arrayOfArrays[i] as! NSMutableArray
for object in self.arrayOfFoodItems {
let name = object["itemName"] as! String
if name.lowercaseString.hasPrefix(prefix.lowercaseString) {
array.addObject(object)
}
}
}
NSOperationQueue.mainQueue().addOperationWithBlock { () -> Void in
self.tableView.reloadData()
}
}