使用UserRecordID获取CloudKit用户记录

时间:2016-07-30 13:39:53

标签: ios iphone database swift cloudkit

问题是在获取UserRecordID时从用户记录中获取数据。

获取用户ID的方法:     post.creatorUserRecordID?.recordName

我的用户记录类型包含像用户名这样的列,因此,我需要为具体用户解析它们。有可能吗?

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题并且你已经有了CKRecordID。你所要做的就是使用你得到的CKRecordID来获取RecordWithID。

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")
        }
    }
})