Pedro de Sá
Pedro de Sá

Reputation: 780

How to fetch a CKShare?

I can generate a share and fetch it normally by clicking the link that the share generates, but as soon as I close the app and open it again, it's all gone! What I want to do is to access the share without the link... By opening the app, load the content in the share that was already accepted in the first time. Does somebody knows how to do it?

Thanks in advance

Upvotes: 2

Views: 859

Answers (1)

Pedro de Sá
Pedro de Sá

Reputation: 780

I got it! First you need to fetch the CKRecordZone of that share you want.

let sharedData = CKContainer.default().sharedCloudDatabase
    sharedData.fetchAllRecordZones { (recordZone, error) in
        if error != nil {
            print(error?.localizedDescription)
        }
        if let recordZones = recordZone {
            //Here you'll have an array of CKRecordZone that is in your SharedDB
        }
    }

Now, you only need to fetch the CKShare as you'd do to a regular CKRecord:

func asdasdasa(id: CKRecordZoneID) {

    ctUsers = [CKRecord]()
    let sharedData = CKContainer.default().sharedCloudDatabase
    let predicate = NSPredicate(format: "TRUEPREDICATE")
    let query = CKQuery(recordType: "Elder", predicate: predicate)

    sharedData.perform(query, inZoneWith: id) { results, error in
        if let error = error {
            DispatchQueue.main.async {
                print("Cloud Query Error - Fetch Establishments: \(error)")
            }
            return
        }
        if let users = results {
            print(results)
            self.ctUsers = users
            print("How many shares in cloud: \(self.ctUsers.count)")
            if self.ctUsers.count != 0 {
                // Here you'll your Shared CKRecords!
            }
            else {
                print("No shares found")
            }
        }
    }
}

Upvotes: 3

Related Questions