无法分配用户 ID 作为参考
Posted
技术标签:
【中文标题】无法分配用户 ID 作为参考【英文标题】:Cannot assign userID as reference 【发布时间】:2015-02-16 00:16:35 【问题描述】:我正在尝试为记录分配几个不同的属性。
日期和名称工作正常。 Avatar 是对另一条记录的引用,也可以正常工作。但后来我尝试为用户分配一个引用,我得到了对屏幕的猛烈抨击。这是我的代码:
var avatarID : CKRecordID!
var userID : CKRecordID!
//avatarID has been assigned value in another function
daMainUser.setObject(CKReference(recordID: avatarID! as CKRecordID, action: CKReferenceAction.None), forKey: "avatar")
println("this is avatarID: \(avatarID!)")
containerY.fetchUserRecordIDWithCompletionHandler
userRecordID, error in
if error != nil
println("error")
else
println("gettin close")
self.userID = userRecordID
println("this is userID: \(self.userID!)")
daMainUser.setObject(CKReference(recordID: self.userID! as CKRecordID, action: CKReferenceAction.None), forKey: "ownerOfThat")
publicDatabase.saveRecord(daMainUser)
record, error in
if error != nil
println(error.localizedDescription)
else
println(record.objectForKey("nickName") as String + " recorded")
当我保存 daMainUser 并查看苹果仪表板时,我看到“头像”引用已成功保存,但没有“ownerOfThat”。这是我从控制台得到的:
this is avatarID: <CKRecordID: 0x7a83d3d0; 79CE1EDC-593D-4AE0-8433-8488F857A302:(_defaultZone:__defaultOwner__)>
gettin close
this is userID: <CKRecordID: 0x7a863910; _8a15e8e6138ba234b62f25cfd3eeb66e:(_defaultZone:__defaultOwner__)>
John Doe recorded
这让我相信 userID 和来自另一条记录的 ID 不是同一种 ID。但我不知道为什么以及该怎么做。 提前谢谢你
【问题讨论】:
【参考方案1】:我认为你的括号错了。应该是:
var avatarID : CKRecordID!
var userID : CKRecordID!
//avatarID has been assigned value in another function
daMainUser.setObject(CKReference(recordID: avatarID! as CKRecordID, action: CKReferenceAction.None), forKey: "avatar")
println("this is avatarID: \(avatarID!)")
containerY.fetchUserRecordIDWithCompletionHandler
userRecordID, error in
if error != nil
println("error")
else
println("gettin close")
self.userID = userRecordID
println("this is userID: \(self.userID!)")
daMainUser.setObject(CKReference(recordID: self.userID! as CKRecordID, action: CKReferenceAction.None), forKey: "ownerOfThat")
publicDatabase.saveRecord(daMainUser)
record, error in
if error != nil
println(error.localizedDescription)
else
println(record.objectForKey("nickName") as String + " recorded")
现在您在获取记录之前保存它。
【讨论】:
谢谢 Edwin,你又为我保存了一次屏幕。以上是关于无法分配用户 ID 作为参考的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Flutter 中从 Firebase Auth 获取用户 ID?