shared database zones cannot be found

327 views Asked by At

I have sucessfully shared a record from private database customzone.

share url Optional(https://www.icloud.com/share/0N9smwzXZ0gfumZv8jyVo7uag)

When I print shared record I get this:

record completion <CKShare: 0x106702100; participants=(
    "<CKShareParticipant: 0x2817115e0; hasProtectionInfo=1, isCurrentUser=YES, participantID=E742CBD3-41C9-4A9E-A392-08914E8C1D37, permission=readWrite, role=owner, acceptanceStatus=Accepted, identity=<CKUserIdentity: 0x281815ce0; lookupInfo=<CKUserIdentityLookupInfo: 0x2832ba910; [email protected]>, cached=0, nameComponents=viswanatha, userID=<CKRecordID: 0x283c6f940; recordName=__defaultOwner__, zoneID=_defaultZone:__defaultOwner__>, contactIdentifiers={\n    items =     (\n    );\n}>>"
), allowsReadOnlyParticipantsToSeeEachOther=YES, publicPermission=none, recordChangeTag=2z, rootRecordID=<CKRecordID: 0x283c39360; recordName=645B675E-121F-4DC8-B86D-8968C8DE7B1C-9651-00000ED7A0D0E95D, zoneID=userProfileZone:__defaultOwner__>, routingKey=0N9, recordID=Share-967C18F4-43C4-4958-9D40-6D890DD205AD:(userProfileZone:__defaultOwner__), recordType=cloudkit.share, etag=2z> and nil
record completion <CKRecord: 0x10bc05ac0; recordID=645B675E-121F-4DC8-B86D-8968C8DE7B1C-9651-00000ED7A0D0E95D:(userProfileZone:__defaultOwner__), recordChangeTag=30, values={
    asOfDate = "2020-08-01 20:26:44 +0000";
    latitude = "29.9288104125185";
    locationId = "1D2A93A8-A22C-4B2A-9EC6-86F646667F3E-8282-00000CF78F38B1DB";
    locationType = "Household User";
    longitude = "-95.6080147578347";
    profileImage = "<CKAsset: 0x10bc05f60; referenceSignature=<01ebb50c 4bd0e618 bb0f4486 b486145b 30f22689 83>, uploadRank=0, path=/private~/Library/Caches/CloudKit/0fce42f4be876f65546aa1b4549b3658e9291963/Assets/B21AB25E-C07A-4D65-9238-61A17B3E6372.01b1f7f738f239ee006815df2bb2896fade0f08229, size=34656, UUID=B21AB25E-C07A-4D65-9238-61A17B3E6372, signature=<01b1f7f7 38f239ee 006815df 2bb2896f ade0f082 29>, wrappedAssetKey=<24 bytes>>";
    userType = "Consumer";
}, share=<CKReference: 0x283c38ae0; recordID=<CKRecordID: 0x283c390c0; recordName=Share-967C18F4-43C4-4958-9D40-6D890DD205AD, zoneID=userProfileZone:__defaultOwner__>>, recordType=infrastructure> and nil

Then I fetch RecordZones

 func fetchRecordZones(completion: @escaping (CKRecordZoneID?, Error?) -> Void) {
        
        var fetchedRecordZones: [CKRecordZoneID : CKRecordZone]? = nil
        let fetchZonesOperation = CKFetchRecordZonesOperation.fetchAllRecordZonesOperation()
        fetchZonesOperation.fetchRecordZonesCompletionBlock = { (recordZones: [CKRecordZoneID : CKRecordZone]?, error: Error?) -> Void in
            guard error == nil else {
                completion(nil, error)
                return
            }
            
            if let recordZones = recordZones {
                fetchedRecordZones = recordZones
                for recordID in recordZones.keys {
                    print(recordID.zoneName)
                    if (recordID.zoneName == Cloud.SharedZone.UserProfile.ZoneName) {
                        completion(recordID, nil)
                    }
                }
            }
            completion(nil, nil)
        }
        fetchZonesOperation.qualityOfService = .utility
        self.sharedDB?.add(fetchZonesOperation)
    }

I get no recordZones

I go to coud Kit and look at the shared database and fetch zones with fetch with change Token checkbox enabled I get name: userProfileZone owner record name:_9d4825db9b0aa911655e420ec0016129

Looks like it has a zone in shared database. At least seems like, so why am I not getting zones from shared database?

//Sharing code

var lookupInfos =  [CKUserIdentity.LookupInfo]()
let creatorID = rootRecord.creatorUserRecordID
self.defaultContainer?.discoverUserIdentity(withUserRecordID: creatorID!) { [weak self] (userIdentity, error) in
    guard error == nil else {
        if let ckerror = error as? CKError {
            self!.aErrorHandler.handleCkError(ckerror: ckerror)
        }
        completion(false, error)
        return
    }
    if let userIdentity = userIdentity {
        lookupInfos.append(userIdentity.lookupInfo!)
        let share = CKShare(rootRecord: rootRecord)
        share[CKShareTitleKey] = "Infrastructure" as CKRecordValue
        //share[CKShareThumbnailImageDataKey] = shoppingListThumbnail as CKRecordValue
        share[CKShareTypeKey] = "com.nr2r.infrastructure" as CKRecordValue
        if let lookupInfo = userIdentity.lookupInfo {
            let op: CKFetchShareParticipantsOperation = CKFetchShareParticipantsOperation(userIdentityLookupInfos: [lookupInfo])
            op.fetchShareParticipantsCompletionBlock = { error in
                if let error = error {
                    print("error: ", error)
                }
            }
            op.shareParticipantFetchedBlock = { participant in
                participant.permission = .readWrite
                share.addParticipant(participant)
                let modOp: CKModifyRecordsOperation = CKModifyRecordsOperation(recordsToSave: [rootRecord, share], recordIDsToDelete: nil)
                modOp.savePolicy = .ifServerRecordUnchanged
                modOp.perRecordCompletionBlock = {record, error in
                    print("record completion \(record) and \(String(describing: error))")
                }
                modOp.modifyRecordsCompletionBlock = {records, recordIDs, error in
                    guard let ckrecords: [CKRecord] = records, let record: CKRecord = ckrecords.first, error == nil else {
                        print("error in modifying the records " + error!.localizedDescription)
                        completion(false, error)
                        return
                    }
                    if let records = records {
                        for record in records {
                            print ("recordType: \(record.recordType)")
                            if (record.recordType == "cloudkit.share") {
                                self!.sharedCKRecordZoneID = record.recordID.zoneID
                            }
                            print ("recordName: \(record.recordID.recordName)")
                            print ("zoneID: \(record.recordID.zoneID.zoneName)")
                            print ("zoneID: \(record.recordID.zoneID.ownerName)")
                        }
                    }
                    if let anURL = share.url {
                        print("share url \(String(describing: share.url))")
                        completion(true, nil)
                    }
                }
                self?.privateDB?.add(modOp)
            }
            self?.defaultContainer?.add(op)
        }
    } else {
        completion(false, nil)
    }
}

More edits to show how to retrieve shared record:

let op = CKFetchShareMetadataOperation(shareURLs: [aURL])
op.perShareMetadataBlock = { shareURL, shareMetadata, error in
    if let shareMetadata = shareMetadata {
        if shareMetadata.participantStatus == .accepted {
            let query = CKQuery(recordType: Cloud.Entity.Infrastructure, predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
            let zone = CKRecordZoneID(zoneName: Cloud.PrivateZone.UserProfile.ZoneName, ownerName: (shareMetadata.ownerIdentity.userRecordID?.recordName)!)
            self.sharedDB?.perform(query, inZoneWith: zone, completionHandler: { (records, error) in
                if let ckerror = error as? CKError {
                    self.aErrorHandler.handleCkError(ckerror: ckerror)
                    completion(nil, error)
                } else if let records = records, let firstRecord = records.first {
                    completion(firstRecord, nil)
                }
            })
        }
}

Fetching shared database zones return zero

self.sharedDB?.fetchAllRecordZones(completionHandler: { (ckrecordZones, error) in
            guard error == nil else {
                if let ckError = error as? CKError {
                    self.aErrorHandler.handleCkError(ckerror: ckError)
                }
                completion (nil, error)
                return
            }
            if let recordZones = ckrecordZones {
                for i in 0 ..< recordZones.count{
                    // find the zone you want to query
                    if recordZones[i].zoneID.zoneName ==  Cloud.SharedZone.UserProfile.ZoneName {
                        completion (recordZones[i].zoneID, nil)
                    }
                }
            }
            completion (nil, nil)
        })
 

   
0

There are 0 answers