How do I append data from a child item in Firebase database? Below is my code.
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)
}
}
Here's a picture of my Firebase data structure:
Data should only be obtained once when the ViewController
is opened and then appended into the array. Thanks for your help.
Edit: The intention is to obtain data from the firebase child 'medals' and append it to the local array "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)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.