CloudKit CKQueryOperation doesn't get all records - swift

In CloudKit RecordType is more than 100 records. Following code gets from these only 11 and they are not first 11 records, they are picked randomly from beginning, center and at the end of records. I can't get whats wrong in code.
EDIT : I got it working by changing .reseltsLimit to 5000!
let cloudContainer = CKContainer.default()
let publicDatabase = cloudContainer.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "Sijainti", predicate: predicate)
var queryOperation = CKQueryOperation(query: query)
queryOperation.queuePriority = .veryHigh
queryOperation.resultsLimit = 50
queryOperation.desiredKeys = ["Koordinaatit"]
queryOperation.recordFetchedBlock = { (record) -> Void in
self.sijainnitArray.append(record)
}
queryOperation.queryCompletionBlock = { (cursor, error) -> Void in
if error != nil {
print("Failed to get data")
return
}
if cursor != nil {
let newQueryOperation = CKQueryOperation(cursor: cursor!)
newQueryOperation.cursor = cursor
newQueryOperation.resultsLimit = queryOperation.resultsLimit
newQueryOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
queryOperation = newQueryOperation
publicDatabase.add(queryOperation)
return
}
}
publicDatabase.add(queryOperation)

Related

Fetched data from private CloudKit database not returning from function

I'm trying to fetch some data from a private ClouKit database. The query and predicate is working fine because I can see at print(data) // 1 that the array is filled with all the data. But as soon as it leaves perform(){} the array is resetted to it's at the top defined values and it just returns them.
func fetchUserRecord() -> [Double] {
var data: [Double] = [0, 1, 2] //demo data
let aWeekAgo = Date().addingTimeInterval(-604800)
let privateDB = CKContainer.default().privateCloudDatabase
let predicate = NSPredicate(format: "Date > %#", aWeekAgo as NSDate)
let query = CKQuery(recordType: "ProgressionValue", predicate: predicate)
privateDB.perform(query, inZoneWith: nil) { records, error in
guard let records = records else { return }
for record in records {
data.append(record["Value"] as! Double)
}
data.append(0.0)
print(data) // 1
}
print(data) // 2
return data
}
Here are the outputs:
print(data) //1:
[0.0, 1.0, 2.0, {tons of doubles}, 0.0]
print(data) //2:
[0.0, 1.0, 2.0]
What am I missing?
Try the new synch tools;
func fetchUserRecord() async throws -> [Double]
{
var data: [Double] = [0, 1, 2] // you don't really want the
let aWeekAgo = Date().addingTimeInterval(-604800)
let privateDB = CKContainer.default().privateCloudDatabase
let predicate = NSPredicate(format: "Date > %#", aWeekAgo as NSDate)
let query = CKQuery(recordType: "ProgressionValue", predicate: predicate)
let (values, cursor) = try await privateDB.records(matching: query, resultsLimit: 100)
for r in values
{
if let rec = try? r.1.get()
{
data.append(rec["value"] as Double)
}
}
return x
}
Call it like this;
Task
{
do {
data = try await fetchUserRecord()
}
catch
{
print(error)
}
}

Saving & Fetching CloudKit References

I'm having trouble creating with CloudKit References. Data is being saved into CloudKit but its not referencing its parent (list). Don't know what i'm doing wrong, any help would be much appreciated!
Saving Method
var list: CKRecord?
var item: CKRecord?
#objc func save() {
let name = nameTextField.text! as NSString
//Fetch Private Database
let privateDatabase = CKContainer.default().privateCloudDatabase
if item == nil {
//Create Record
item = CKRecord(recordType: RecordTypeItems)
//Initialization Reference
guard let recordID = list?.recordID else { return }
let listReference = CKRecord.Reference(recordID: recordID, action: .deleteSelf)
item?.setObject(listReference, forKey: "list")
}
item?.setObject(name, forKey: "name")
//Save Record
privateDatabase.save(item!) { (record, error) in
DispatchQueue.main.sync {
self.processResponse(record: record, error: error)
}
}
}
Fetch Method
var list: CKRecord!
var items = [CKRecord]()
private func fetchItems() {
//Fetch Private Database
let privateDatabase = CKContainer.default().privateCloudDatabase
//Initialize Query
guard let recordID = list?.recordID else { return }
let reference = CKRecord.Reference(recordID: recordID, action: .deleteSelf)
let query = CKQuery(recordType: RecordTypeItems, predicate: NSPredicate(format: "list == %#", [reference]))
//Configure Query
query.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
//Peform Query
privateDatabase.perform(query, inZoneWith: nil) { (records, error) in
DispatchQueue.main.sync {
self.processResponseForQuery(records: records, error: error)
}
}
}
Where you are creating your query to retrieve items referencing the list, should the list reference in the predicate format string be inside an array? If you create the item's reference like item?.setObject(listReference, forKey: "list"), CloudKit will infer the list field to be a single CKRecord.Reference, so the query would be:
let query = CKQuery(recordType: RecordTypeItems, predicate: NSPredicate(format: "list == %#", reference))

Swift CloudKit not working with Cellular (Time exceeded)

I'm using following code to fetch Data from iCloud:
func fetchShoppingList() {
let container = CKContainer.default()
let publicDB = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "ShoppingList", predicate: predicate)
let operation = CKQueryOperation(query: query)
operation.allowsCellularAccess = true
operation.qualityOfService = .userInitiated
publicDB.add(operation)
publicDB.perform(query, inZoneWith: nil) { [unowned self] results, error in
if error != nil {
print(error)
}
else {
for var value in results! {
let shoppingListEntry = ShoppingListEntry()
shoppingListEntry.index = value.value(forKey: "index") as! Int
shoppingListEntry.product = value.value(forKey: "product") as! String
shoppingListEntry.amount = value.value(forKey: "amount") as! Int
shoppingListEntry.priority = value.value(forKey: "priority") as! Int
if value.value(forKey: "isSelected") as! String == "true" {
shoppingListEntry.isSelected = true
}
else {
shoppingListEntry.isSelected = false
}
self.shoppingListEntrys.append(shoppingListEntry)
}
OperationQueue.main.addOperation({ () -> Void in
self.tableViewShoppingList.reloadData()
})
}
}
}
Everything works fine if my phone is connected via Wifi, but if I'm using Cellular I get following error: CKError 0x170244e30: "Network Failure" (4/-1001); "Zeitüberschreitung bei der Anforderung." So there seems to be a problem with time exceeding. I looked for a solution and found a post, saying I have to add the operation lines but nothing changed.
Can anybody help me please?

CloudKit Query Operation only returns 300 results

I am currently setting up CloudKit as a replacement to Parse and need to download all of my user records. I currently have around 600 records but I am only receiving 300.
I'm using a custom record zone called "User" rather than the default "Users" record zone as this app will only ever be tied to one appID.
The code I am using is based on the answer to the below question but it's not working for me. It seems that the query operation does not run when the cursor is nil as the print(userArray) is never called. Thanks in advance for your help!
CKQuery from private zone returns only first 100 CKRecords from in CloudKit
func queryAllUsers() {
let database = CKContainer.defaultContainer().privateCloudDatabase
let query = CKQuery(recordType: "User", predicate: NSPredicate(value: true))
let queryOperation = CKQueryOperation(query: query)
queryOperation.recordFetchedBlock = self.createUserObject
queryOperation.queryCompletionBlock = { cursor, error in
if cursor != nil {
print("there is more data to fetch")
let newOperation = CKQueryOperation(cursor: cursor!)
newOperation.recordFetchedBlock = self.createUserObject
newOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
database.addOperation(newOperation)
} else {
print(userArray) //Never runs
}
}
database.addOperation(queryOperation)
}
func createUserObject(record: CKRecord) {
let name = record.objectForKey("Name") as! String!
let company = record.objectForKey("Company") as! String!
let dateInductionCompleted = record.objectForKey("DateInductionCompleted") as! NSDate!
var image = UIImage()
let imageAsset = record.objectForKey("Image") as! CKAsset!
if let url = imageAsset.fileURL as NSURL? {
let imageData = NSData(contentsOfURL:url)
let mainQueue = NSOperationQueue.mainQueue()
mainQueue.addOperationWithBlock() {
image = UIImage(data: imageData!)!
userArray.append(User(name: name, company: company, image: image, dateInductionCompleted: dateInductionCompleted))
}
}
print(userArray.count)
}
UPDATE
The question has been answered, it was possibly an inherent bug when using a cursor for large queries. The code now works by using a recursive function, working code below:
func queryRecords() {
let database = CKContainer.defaultContainer().privateCloudDatabase
let query = CKQuery(recordType: "User", predicate: NSPredicate(value: true))
let queryOperation = CKQueryOperation(query: query)
queryOperation.qualityOfService = .UserInitiated
queryOperation.recordFetchedBlock = populateUserArray
queryOperation.queryCompletionBlock = { cursor, error in
if cursor != nil {
print("There is more data to fetch")
self.fetchRecords(cursor!)
}
}
database.addOperation(queryOperation)
}
func fetchRecords(cursor: CKQueryCursor?) {
let database = CKContainer.defaultContainer().privateCloudDatabase
let queryOperation = CKQueryOperation(cursor: cursor!)
queryOperation.qualityOfService = .UserInitiated
queryOperation.recordFetchedBlock = populateUserArray
queryOperation.queryCompletionBlock = { cursor, error in
if cursor != nil {
print("More data to fetch")
self.fetchRecords(cursor!)
} else {
print(userArray)
}
}
database.addOperation(queryOperation)
}
func populateUserArray(record: CKRecord) {
let name = record.objectForKey("Name") as! String!
let company = record.objectForKey("Company") as! String!
let dateInductionCompleted = record.objectForKey("DateInductionCompleted") as! NSDate!
var image = UIImage()
let imageAsset = record.objectForKey("Image") as! CKAsset!
if let url = imageAsset.fileURL as NSURL? {
let imageData = NSData(contentsOfURL:url)
let mainQueue = NSOperationQueue.mainQueue()
mainQueue.addOperationWithBlock() {
image = UIImage(data: imageData!)!
userArray.append(User(name: name, company: company, image: image, dateInductionCompleted: dateInductionCompleted))
}
}
print(userArray.count)
}
Could you try setting:
queryOperation.qualityOfService = .UserInitiated
This will indicate that your user interaction requires the data.
Otherwise it could happen that de request is ignored completely.
As discussed below the actual answer was that you should not re-use completion blocks. Instead you should create a recursive function for fetching the next records from a cursor. A sample of that can be found at: EVCloudKitDao

How can I speed up performQuery in Swift?

So I am creating this app that uses CloudKit to save and fetch images and text from the Cloud. The problem is that I can only access the results after the whole fetch is done. I would like to be able to fetch each record individually as it is fetched. Here is the code.
func fetchPost() {
spinner.startAnimating()
if imageView.image != nil {
spinner.alpha = 0
}
var imageData = [UIImage]()
var text = [String]()
let predicate = NSPredicate(value: true)
let sort = NSSortDescriptor(key: "creationDate", ascending: false)
let query = CKQuery(recordType: "Post",
predicate: predicate)
query.sortDescriptors = [sort]
publicDB.performQuery(query, inZoneWithID: nil) {
results, error in
if error != nil {
dispatch_async(dispatch_get_main_queue()) {
println("Query failed")
return
}
} else {
println("test")
var number = 0
for record in results {
if let pictureRecord = record as? CKRecord {
let post = Post(record: pictureRecord, database: self.publicDB)
let postImageData = post.imageData
let postText = post.text
self.images.append(UIImage(data: postImageData)!)
self.texts.append(postText)
println("\"\(postText)\" is the text. Fetch successful.")
if number == 0 {
self.imageView.image = self.images[0]
self.nameLabel.text = self.texts[0]
}
++number
} else {
println("Records failed")
}
}
}
self.spinner.stopAnimating()
self.spinner.alpha = 1
}
}
Thanks!
If you use CKQueryOperation you can set a callback (recordFetchedBlock) that will be called for each record as it is fetched from the server.