简体   繁体   中英

Code only retrieving one value from data in Firebase

As the title suggests, I'm trying to retrieve some data from firebase database, but my code's not working. I have three children (I guess that's how you call them) inside "Posts" called "title", "description" and "username" and I'm trying to get all of them and append them into a variable to use them later, but it only retrieves the first value of each of them, despite the fact that there are like 5 values. Anyone knows why? By the way, I'm calling upon this function on my ViewDidLoad.

let postDB = Database.database().reference().child("Posts")
postDB.queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
    if let snapshotValue = snapshot.value as? NSDictionary {

    let postTitle = snapshotValue["title"] as? String
    let postUsername = snapshotValue["username"] as? String
    let postDesc = snapshotValue["description"] as? String

    let postArray = postStruct(title: postTitle, description: postDesc, username: postUsername)
    self.newPost.insert(postArray, at: 0)

    }

我的 Firebase 数据库。

Try this – the code replaces what you currently have in the snapshot handler.

if let firebaseList = snapshot.children.allObjects as? [FIRDataSnapshot] {
        if let swiftList = snapshot.value as? [String:AnyObject] {
            for firebaseItem in firebaseList {
                let childID = firebaseItem.key as String
                let swiftItem = swiftList[childID]
                let postTitle = swiftItem?["title"] as? String
                let postUsername = swiftItem?["username"] as? String
                let postDesc = swiftItem?["description"] as? String
                let postArray = postStruct(title: postTitle, description: postDesc, username: postUsername)
                self.newPost.insert(postArray, at: 0)
            }
        }
    }
}
import UIKit
import FirebaseDatabase

class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    // MARK: - variables
    var postDB: DatabaseReference!

    override func viewDidLoad() {
        super.viewDidLoad()

        // getting a node from database //
        postDB = Database.database().reference().child("Posts")

        // observing data changes //
        postDB.observe(DataEventType.value) { (dataSnapshot) in
            self.postArray.removeAll()
            if dataSnapshot.childrenCount > 0 {
                for post in dataSnapshot.children.allObjects as! [DataSnapshot] {
                    let object = post.value as! [String: Any]
                    let description = object["description"] as! String
                    let title = object["title"] as! String
                    let userName = object["username"] as! String
                    let model = postStruct(title: title, description: description, username: userName))
                    self.postArray.append(model)
                }
            }
            self.tableView.reloadData()
        }
    }
}

Worked for me. It gets all the values now you just have to put them in an array

postDB.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
            @Override
            public void onComplete(@NonNull @NotNull Task<DataSnapshot> task) {
                if(!task.isSuccessful()){
                    Log.e("firebase", "Error getting data; ", task.getException());
                }else{

                    Log.d("firebase", String.valueOf(task.getResult().getValue()));
                }
            }
        });

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM