4

The question is to fetch data from User Records when UserRecordID is fetched.

Method to get User ID: post.creatorUserRecordID?.recordName

My Users Record Type contains columns like username, so, I need to parse them for a concrete user. Is it possible somehow?

2 Answers 2

2

If I understood your question and you already have a CKRecordID. All you got to do then is to fetchRecordWithID using this CKRecordID you got.

let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
publicDatabase.fetchRecordWithID(recordId, completionHandler: { (fetchRecord: CKRecord?, fetchError: NSError?) in
    if let error = fetchError
    {
        // error getting user record, try again
        print("-> cloudKitFetchUserRecord - error fetching user record - Error \(error)")
    }
    else
    {
        if let record = fetchRecord
        {
            if record.recordType == CKRecordTypeUserRecord
            {
                // valid record
                print("-> cloudKitFetchUserRecord - fetching user record - valid record found - \(record.recordID.recordName))")

               // unwrap your values - on your case username
               if let object = record.objectForKey("username") as? Bool
                {
                    // do something with object
                }

            }
            else
            {
                // not valid record
                print("-> cloudKitFetchUserRecord - fetching user record - The record that came back is not a CKRecordTypeUserRecord")
            }
        }
        else
        {
            // record nil
            print("-> cloudKitFetchUserRecord - fetching user record - fetch record returned nil")
        }
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

It is simpler now using async/await in Swift 5

func getUserID() async {
    
    let container = CKContainer(identifier: "iCloud.com.XXX.XXXX")
    // CKContainer.defaultContainer().publicCloudDatabase // or default public container
    
    do {
        let userRecordID = try await container.userRecordID()
        print("recordName: \(userRecordID.recordName)")
    }
    catch {
        print("Error: \(error)")
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.