Swift Error: containerIdentifier can not be nil (CKexception)

Viewed 103

I'm new with Swift. And I have my first project with CloudKit. But unfortunately I get a error wit getting data from CloudKit:

import Foundation
import CloudKit

class CKRelation {
    static let database = CKContainer.default().publicCloudDatabase

    class func fetch(completion: @escaping (Result<[Relation], Error>) -> ()) {
        let predicate = NSPredicate(value: true)
        let name = NSSortDescriptor(key: "LastName", ascending: true)
        let query = CKQuery(recordType: "Clients", predicate: predicate)
        query.sortDescriptors = [name]

        let operation = CKQueryOperation(query: query)
        operation.desiredKeys = ["FirstName", "Initials", "LastName", "MiddleName", "RelationId"]
        operation.resultsLimit = 50

        var newRelations = [Relation]()

        operation.recordFetchedBlock = { record in
            var relation = Relation()
            relation.recordID = record.recordID
            relation.FirstName = record["FirstName"] as! String
            relation.Initials = record["Initials"] as! String
            relation.LastName = record["LastName"] as! String
            relation.MiddleName = record["MiddleName"] as! String
            relation.RelationId = record["RelationId"] as! Int
            
            newRelations.append(relation)
        }

        operation.queryCompletionBlock = { (cursor, error) in
            DispatchQueue.main.async {
                if let error = error {
                    completion(.failure(error))
                } else {
                    completion(.success(newRelations))
                    print("data")
                }
            }
        }
        database.add(operation)
    }

I get this error:

*** Terminating app due to uncaught exception 'CKException', reason: 'containerIdentifier can not be nil'
terminating with uncaught exception of type CKException

I can not find anything on the internet. Hopefully somebody can help me.

0 Answers
Related