Querying shared records in a ckzone in the private database returns nothing

300 views Asked by At

Swift 4.2 iOS 11.x

I must have missed something cause this surely should work. I got this code that queries the private database in a custom zone. It returns nothing despite the fact that I have records there. I added indexes to everything! I also changed the roles in permissions so that any authenticated user could read records.

public func cleanUpImages(zone2U:String) {
    var records2Delete:[CKRecord.ID] = []
    let zone2D = CKRecordZone(zoneName: zone2U)

    let predicate = NSPredicate(value: true)
    let query = CKQuery(recordType: remoteRecords.notificationMedia, predicate: predicate)
    let operation = CKQueryOperation(query: query)
    operation.recordFetchedBlock = { record in
        records2Delete.append(record.recordID)
    }
    operation.queryCompletionBlock = { cursor, error in
        print(records2Delete.count)
    }
    CKContainer.default().privateCloudDatabase.add(operation)

notificationMedia is a global static var that I also use to save said records, so it cannot be wrong/different.

2

There are 2 answers

4
rmaddy On

Set the zoneID of operation because right now you are querying the default zone.

let operation = CKQueryOperation(query: query)
operation.zoneID = zone2D.zoneID
0
user3069232 On

I think I found the answer. I was using a deprecated method to save my record.

let customRecord = CKRecord(recordType: remoteRecords.notificationMedia, zoneID: zone2D.zoneID)

I changed it to this..

let customID = CKRecord.ID(recordName: remoteRecords.notificationMedia, zoneID: zone2D.zoneID)
let customRecord = CKRecord(recordType: remoteRecords.notificationMedia, recordID: customID)

Now I find my records in the sharedDatabase on the cloudKit database and when I use the method shown above the zone set as pointed out by rmaddy I find my records.