有序数组闪烁效果?

时间:2018-06-11 20:31:35

标签: swift uitableview firebase firebase-realtime-database tableview

当我从有序数组加载数据时,表视图在加载时会遍历每个单元格。似乎表视图首先加载,然后数组被排序,这导致闪烁效果。反过来,这也会混淆每个细胞所具有的图像。

在快照获取数据后调用

     postReference.observeSingleEvent(of: .value, with: { (snapshot) in

                if let value = snapshot.value as? NSDictionary {
                let post = Post()
                let post_content = value["post"] as? String ?? "Content not found"

                post.revealDate = value["revealedDate"] as? Double ??  0000000         
                            self.postList.append(post)
                    }

                    self.postList.sort() { $0.revealDate! > $1.revealDate! }
                    self.tableView.reloadData()

            }

并加载单元格

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if queryComplete == true{


 let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PostTableViewCell
            //set cell content
            let contentOfCellPost = postList[indexPath.row]
            cell.label?.text = contentOfCellPost.post_words
            return cell

This is how the posts flicker when the table view loads

似乎表格视图正在将每个单元格作为第一个单元格加载,然后再对其进行排序。

修改

整个firebase初始数据负载观察:

func fetchFeed () {
    let UID = Auth.auth().currentUser?.uid
    feedReference = Database.database().reference().child("feeds").child(UID!)
    feedReference.keepSynced(true)

    self.postRefHandle = feedReference.observe(.childAdded) { (snapshot) in
       let postID = snapshot.key
       let postReference = Database.database().reference().child("posts").child(postID)

       postReference.observeSingleEvent(of: .value, with: { (snapshot) in

           if let value = snapshot.value as? NSDictionary {
               let post = Post()
               let post_content = value["post"] as? String ?? "Content not found"
               post.Reveals = value["reveals"] as? Int ?? 10000000
               post.revealsRequired = value["revealsRequired"] as? Int ?? 100000
               post.post_words = post_content
               post.postID = postID
               post.Revealed = value["Revealed"] as? String ?? "Revealed or Not not found"
               post.revealsRequired = value["revealsRequired"] as? Int ?? 1000000000
               post.revealDate = value["revealedDate"] as? Double ??  0000000
               post.timeOfDeletion = value["timeOfDeletion"] as? Int ?? 100000000000

               if snapshot.hasChild("information") {
                   if let infoName = snapshot.childSnapshot(forPath: "information").value as? NSDictionary {
                       post.name = infoName["posterName"] as? String ?? "Poster not found"
                       post.profileImageLink = infoName["profileImage"] as? String ?? "nil"
                       post.revealedBool = true
                       self.postList.append(post)
                   }
               } else {
                  let date = Date()
                  let currentTime = date.timeIntervalSince1970 * 1000
                  let timeToDelete = Int(post.timeOfDeletion!) - Int(currentTime)
                  post.revealedBool = false
                  post.name = String(timeToDelete)
                  self.postList.append(post)
              }

              self.postList.sort() { $0.revealDate! > $1.revealDate! }
              self.tableView.reloadData()
        }
    })
  }
}

观察帖子的变化:

 postReference.observe(.value, with: { snapshot in



                if self.queryComplete == true {
                    let changedKey = snapshot.key
                    if let index = self.postList.index(where: {$0.postID == changedKey}) {
                        let indexPath = IndexPath(row: index, section: 0)
                        let changedPost = self.postList[index]
                        if let value = snapshot.value as? NSDictionary {

                            let newReveals = value["reveals"]  as? Int
                            changedPost.Reveals = newReveals
                            //update the values
                            self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
                        }
                        if let value = snapshot.value as? NSDictionary {
                            let newRevealStatus = value["Revealed"]  as? String ?? "noRevealStatusChange"
                            changedPost.Revealed = newRevealStatus

                            if changedPost.Revealed == "true" {
                                changedPost.revealedBool = true
                                changedPost.revealDate = value["revealedDate"] as? Double ?? 00000


                                if let newName = snapshot.childSnapshot(forPath: "information").value as? NSDictionary {

                                    changedPost.name = newName["posterName"] as? String ?? "Poster not found"
                                    changedPost.profileImageLink = newName["profileImage"] as? String ?? "nil"
                                    self.postList.sort() { $0.revealDate! > $1.revealDate! }

                                    self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
                                }


                            }

                        }
                    }
                }

                 self.queryComplete = true
        } )

JSON示例:

"feeds" : {
    "3ASP4M5mkTPGPO1qQhfVKXsr6Qf2" : {
        "-L65VFpW2cYIMxG2e9Ll" : "true",
        "-L65VH5jkKPYAguzgqpn" : "true"
    }
"posts" : {
    "-L7CpeKT2lzsPALAfNti" : {
        "Revealed" : "true",
        "datePosted" : "2018-03-10 02:56:33 +0000",
        "information" : {
            "posterID" : "BmVot3XHEpYwMNtiucWSb8XPPM42",
            "posterName" : "tester",
            "profileImage" : "nil"
        },

编辑:使用较少观察者的修改代码:

func fetchFeed () {

    let UID = Auth.auth().currentUser?.uid
    feedReference = Database.database().reference().child("feeds").child(UID!)
    feedReference.keepSynced(true)

    self.postRefHandle = feedReference.observe(.childAdded) { (snapshot) in
    let postID = snapshot.key
    let postReference = Database.database().reference().child("posts").child(postID)
        postReference.observe(.value, with: { (snapshot) in
            let postKey = snapshot.key
             if let value = snapshot.value as? NSDictionary {
            if let index = self.postList.index(where: {$0.postID == postKey}) {
                let indexPath = IndexPath(row: index, section: 0)
                let changedPost = self.postList[index]


                    let newReveals = value["reveals"]  as? Int
                    changedPost.Reveals = newReveals
                    //update the values
                    self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)

                    let newRevealStatus = value["Revealed"]  as? String ?? "noRevealStatusChange"
                    changedPost.Revealed = newRevealStatus

                    if changedPost.Revealed == "true" {
                        changedPost.revealedBool = true
                        changedPost.revealDate = value["revealedDate"] as? Double ?? 00000


                        if let newName = snapshot.childSnapshot(forPath: "information").value as? NSDictionary {

                            changedPost.name = newName["posterName"] as? String ?? "Poster not found"
                            changedPost.profileImageLink = newName["profileImage"] as? String ?? "nil"
                            self.postList.sort() { $0.revealDate! > $1.revealDate! }


                            self.roundButton.isHidden = false
                        }


                    }

                  self.tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.none)
            }

                //appending to row for first time (initial load)
            else {
                if let value = snapshot.value as? NSDictionary {
                    let post = Post()
                    let post_content = value["post"] as? String ?? "Content not found"
                    post.Reveals = value["reveals"] as? Int ?? 10000000
                    post.revealsRequired = value["revealsRequired"] as? Int ?? 100000
                    post.post_words = post_content
                    post.postID = postID
                    post.Revealed = value["Revealed"] as? String ?? "Revealed or Not not found"
                    post.revealsRequired = value["revealsRequired"] as? Int ?? 1000000000
                    post.revealDate = value["revealedDate"] as? Double ??  0000000
                    post.timeOfDeletion = value["timeOfDeletion"] as? Int ?? 100000000000

                    if snapshot.hasChild("information"){

                        if let infoName = snapshot.childSnapshot(forPath: "information").value as? NSDictionary {

                            post.name = infoName["posterName"] as? String ?? "Poster not found"
                            post.profileImageLink = infoName["profileImage"] as? String ?? "nil"
                            post.revealedBool = true
                            self.postList.append(post)


                        }
                    }
                    else  {
                        let date = Date()
                        let currentTime = date.timeIntervalSince1970 * 1000
                        let timeToDelete = Int(post.timeOfDeletion!) - Int(currentTime)
                        post.revealedBool = false
                        post.name = String(timeToDelete)

                        self.postList.append(post)

                    }
                    // self.deletePosts()

                    self.postList.sort() { $0.revealDate! > $1.revealDate! }

                    self.tableView.reloadData()



                }

            }
            }
             else {
               Database.database().reference().child("feeds").child(self.userIDCurrent!).child(postID).removeValue()
            }
        })
    }
    self.queryComplete = true
}

1 个答案:

答案 0 :(得分:1)

我将对此采取行动并通过建议另一个方向来解决问题。如果这远离基地,请告诉我,我可以更新或删除。

问题中有很多代码要进行排序,但从表面上看,已经添加了大量观察者,重新加载数据并经常刷新tableView。

从我所看到的,feed节点用于收集数据组;用户感兴趣的一组帖子,一组日期,一组朋友等,每个帖子都是一个Feed,连接到该Feed的每个节点都添加了一个观察者。

我的建议是在更高层次上采取行动 - 而不是通过一个独立的独立观察者来解决每个子节点,让我们在这个父节点内说“嘿Firebase”,如果是孩子节点符合一定的标准,让用户了解它。

因此,让我们利用Firebase深层查询,以更少的观察者来做同样的事情。

举一个示例帖子结构

posts
   post_0
     name: "Jim"
     post: "We have them just where they want us"
     watched_by:
        uid_0: true
        uid_1: true
   post_1
     name: "Spock"
     post: "Nothing unreal exists"
     watched_by:
        uid_1: true
   post_2
     name: "Bones"
     post: "I'm a doctor, not an escalator"
     watched_by
        uid_0: true

并假设具有uid_0的用户进行身份验证。之前,他们决定观看Jim和Bones的帖子。从结构中可以看出,我们在每个帖子中都有一个watched_by节点,并在该节点上添加了uid_0:true。

所以,现在让我们让uid_0观察帖子节点,了解他们感兴趣的帖子的变化:

let postsRef = self.ref.child("posts")
let watchedByQuery = postsRef.queryOrdered(byChild: "/watched_by/uid_0").queryEqual(toValue: true)
watchedByQuery.observe(.childChanged, with: { snapshot in
    let dict = snapshot.value as! [String: Any]
    let post = dict["post"] as! String
    print(snapshot.key, post)
})

所以,如果Bones登录并更改了他的帖子,我就是医生,而不是自动扶梯"我是医生,而不是瓦工"该节点将被发送到uid_0,以便更新ui。

   post_2
     name: "Bones"
     post: "I'm a doctor, not a bricklayer"
     watched_by
        uid_0: true

扩展这一点,我们显然会使用相同的技术添加.childAdded和.childRemoved,并且.childAdded会在每个用户进行身份验证时最初填充我们的tableView dataSource。

假设Bones完全删除了他的帖子(节点)。 watched_by列表中的任何用户都将收到该事件的通知(甚至数百万用户)。发生这种情况时,在代码中,从快照中读取post_2的键,在数据源数组中找到它,删除它并更新tableView。

再次 - 在我的黑暗回答中完全拍摄。