遍历firebase数据库
问题描述:
Swift 3.x iOS 10.遍历firebase数据库
试图理解firebase中的数据库。将一些JSON数据导入我的应用程序并设法读回。这是代码。
let rootRef = Database.database().reference(withPath: "0")
print("\(rootRef.key)")
let nextRef = rootRef.child("identity")
print("\(nextRef)")
nextRef.observe(.value, with: { snapshot in
print("\(snapshot.value)")
})
其中一期工程,我的数据是这样的......
但如何做到这一点,如果我要遍历数据库,看着记录2,记录3等等等等,其中我不确定我有多少记录。
答
好吧,我发现它,还有一些作品......
let rootRef = Database.database().reference()
rootRef.observe(.value, with: { snapshot in
print("dump \(snapshot.children.allObjects)")
})
我张贴后人:)
尼斯的答案)。在这里使用'observeSingleEvent'可能更适合(例如,如果你只想读取* current *数据而不是每次更新)。另外请记住,如果您的Firebase数据库变得庞大,此解决方案可能无法很好地扩展... –