簡體   English   中英

tableView Firebase Swift中的異步數據

[英]Asynchronous Data in tableView Firebase Swift

我在Firebase中最不喜歡的部分出現了問題。 我想從用戶的關注列表中拉一條帖子(每個用戶只有一個,只有一個帖子)。 首先,我創建了一個完成處理程序,以從Firebase獲取所有關注者的列表,並將其存儲在userArray字符串數組中:

func GetUsersInFollowing(completion: @escaping (Bool) -> ()) {
    ref.child("following").queryOrdered(byChild: FIRAuth.auth()!.currentUser!.uid).observeSingleEvent(of: .value, with: { (snapshot) in
        for group in snapshot.children {
            self.userArray.append((group as AnyObject).key)
        }
        completion(true)
    })
}

現在的計划是從userArray的每個元素中提取一個帖子。 這是問題開始的地方。 我在GetUsersInFollowing()完成后立即調用CreatePosts()。

func CreatePosts() {
    for x in userArray {
        var thePost = Post()
        print("1")
        self.ref.child("users").child(x).observeSingleEvent(of: .value, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary
            thePost.fullName = value?["fullname"] as? String ?? ""
            thePost.username = value?["username"] as? String ?? ""
            thePost.profileImageURL = value?["photourl"] as? String ?? ""
            print("2")
        })

        self.ref.child("posts").child(x).observeSingleEvent(of: .value, with: { (snapshot) in
            let value = snapshot.value as? NSDictionary
            thePost.description = value?["description"] as? String ?? ""
            thePost.info = value?["location"] as? String ?? ""
            thePost.postImageURL = value?["photoURL"] as? String ?? ""
            thePost.timePost = value?["timestamp"] as? NSDate
            thePost.upVotes = value?["upvotes"] as? Int ?? 0
        })

        self.postArray.append(thePost)
        self.tableView.reloadData()
    }
}

一切對我來說看起來不錯,但肯定不是。 這是我創建單元格的方法:

func configureCell(post: Post) {
    self.post = post
    self.username.text = post.username
    self.profileImage = post.profileImageURL
    print("3")
    self.fullname.text = post.fullName
    self.timestamp.text = post.timePost
    self.upvotes.text = post.upVotes
    self.location.text = post.location
    self.descriptionText.text = post.description
}

控制台中的輸出會有所不同,但通常我會得到:1 1 3 3 2 2

這個想法是首先從Firebase檢索所有數據,將其添加到發布對象,將該對象附加到數組,然后為下載了數據的該對象創建單元。 即使未檢索到數據,也已經創建了單元。 我認為這就是問題所在。 謝謝您,每個建議都值得贊賞。

您需要內部查詢以同時組合用戶個人資料數據和帖子數據。 像這樣 -

    func CreatePosts() {

        //Using userPostArrayObjFetched as a counter to check the number of data fetched.
        //Remove this code, if you don't want to wait till all the user data is fetched.
        var userPostArrayObjFetched = 0

        for (index,userID) in userArray.enumerated() {

            print("1" + userID)

            self.ref.child("users").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in

                var thePost = Post()

                let value = snapshot.value as? NSDictionary
                thePost.fullName = value?["fullname"] as? String ?? ""
                thePost.username = value?["username"] as? String ?? ""
                thePost.profileImageURL = value?["photourl"] as? String ?? ""
                print("2" + userID)

                self.ref.child("posts").child(userID).observeSingleEvent(of: .value, with: { (snapshot) in
                    let value = snapshot.value as? NSDictionary
                    thePost.description = value?["description"] as? String ?? ""
                    thePost.info = value?["location"] as? String ?? ""
                    thePost.postImageURL = value?["photoURL"] as? String ?? ""
                    thePost.timePost = value?["timestamp"] as? NSDate
                    thePost.upVotes = value?["upvotes"] as? Int ?? 0
                    print("3" + userID)

                    self.postArray.append(thePost)

//                      Uncomment if you want to reload data as fetched from Firebase without waiting for all the data to be fetched.
//                    self.tableView.reloadData()

                    userPostArrayObjFetched += 1

                    if userPostArrayObjFetched == userArray.count{
                         self.tableView.reloadData()
                    }
                })
            })
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM