在 Firebase 中,我们可以像这样创建一个新对象:
Firebase *postRef = [ref childByAppendingPath: @"posts"];
NSDictionary *post1 = @{
@"author": @"gracehop",
@"title": @"Announcing COBOL, a New Programming Language"
};
Firebase *post1Ref = [postRef childByAutoId];
[post1Ref setValue: post1];
这个新帖子可能会保存在如下 URL 中:http://app.firebaseIO.com/posts/$postuid
稍后当用户在某些 UIViewController
中查看帖子时,我们可能想要发表评论。但是,为了发表评论,我们需要访问帖子的 uid。似乎唯一可行的方法是将 uid 保存到我们的 post 对象中?
基本上,将对象的 uid 存储在其中通常是最佳做法吗?
最佳答案
在这种情况下,答案是否定的,您不需要也将唯一标识符( key )存储在节点内,因为它始终可以从 snapshot.key 中获取。
key:value对的值是从snapshot.value中获取的。
可能有一些特殊情况, key 可能需要存储在节点内部,但通常不需要。
从用户节点打印每个键和值的示例
users
uid_0
name: "Kirk"
uid_1
name: "Spock"
代码
let ref = Firebase(url:"https://your-app.firebaseio.com/users")
ref.queryOrderedByChild("name").observeEventType(.ChildAdded, withBlock: { snapshot in
print("The key: \(snapshot.key)") //the key
print("The Value: \(snapshot.value)") //all the values for this key
let specificValue = snapshot.value["name"] as! NSString
print("Specific value: \(specificValue)") //a specific value
})
请注意,在这种情况下,每个用户节点都有一个 uid_x 作为键。这个唯一标识符是从 authData.uid 中获得的。创建用户时。传统上,其他节点中的节点 key 将由 childByAutoId(或非 iOS 的 push())创建
关于你的问题;根据用途,将 firebase 快照数据存储在字典中,并将这些字典(或类)作为 tableView 数据源存储在数组中通常很方便。数组保持顺序。
关于ios - 访问对象的 uid Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36507478/