如何从 Firebase 数据库中的子项追加数据?
How do I append data from a child item in Firebase database?
如何在 Firebase 数据库中追加子项的数据?下面是我的代码。
import UIKit
import Firebase
import FirebaseDatabase
class MedalViewController: UICollectionViewController {
var imagesArray = [String]()
var identities = [String]()
var identities2 = [String]() //Creates an empty array
var imagesArrayGreyed = [String]()
let databaseRef = FIRDatabase.database().reference()
let userID = FIRAuth.auth()?.currentUser?.uid
override func viewDidLoad() {
super.viewDidLoad()
imagesArray = ["1", "2", "3"] //Add all the medal images here
identities = ["Shield", "Tie", "Star"] //Add all the identities here
identities2 = ["Shield"] //Choose what medals the user has.
imagesArrayGreyed = ["1Greyed","2Greyed","3Greyed"] //Add all the greyed images here
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
var newIdentities2 = snapshot.value!["medals"] as! String
// how do I continue on from here?
// ...
}) { (error) in
print(error.localizedDescription)
}
}
这是我的 Firebase 数据结构的图片:
数据应该只在ViewController
打开时获取一次,然后追加到数组中。谢谢你的帮助。
编辑:目的是从 firebase 子 'medals' 获取数据并将其附加到本地数组 "identifier2"
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
if let identities = snapshot.value! as? [String:AnyObject]{
for each in identities.1 as String{
identifier2.append(each)//will append medals
}
}
// ...
}) { (error) in
print(error.localizedDescription)
}
如何在 Firebase 数据库中追加子项的数据?下面是我的代码。
import UIKit
import Firebase
import FirebaseDatabase
class MedalViewController: UICollectionViewController {
var imagesArray = [String]()
var identities = [String]()
var identities2 = [String]() //Creates an empty array
var imagesArrayGreyed = [String]()
let databaseRef = FIRDatabase.database().reference()
let userID = FIRAuth.auth()?.currentUser?.uid
override func viewDidLoad() {
super.viewDidLoad()
imagesArray = ["1", "2", "3"] //Add all the medal images here
identities = ["Shield", "Tie", "Star"] //Add all the identities here
identities2 = ["Shield"] //Choose what medals the user has.
imagesArrayGreyed = ["1Greyed","2Greyed","3Greyed"] //Add all the greyed images here
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
var newIdentities2 = snapshot.value!["medals"] as! String
// how do I continue on from here?
// ...
}) { (error) in
print(error.localizedDescription)
}
}
这是我的 Firebase 数据结构的图片:
数据应该只在ViewController
打开时获取一次,然后追加到数组中。谢谢你的帮助。
编辑:目的是从 firebase 子 'medals' 获取数据并将其附加到本地数组 "identifier2"
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
if let identities = snapshot.value! as? [String:AnyObject]{
for each in identities.1 as String{
identifier2.append(each)//will append medals
}
}
// ...
}) { (error) in
print(error.localizedDescription)
}