0

什么第一次不起作用?:

我获取的数据库条目的显示顺序

我正在运行这个

1st part In ViewDiDLoad

let thisUserRef = Database.database().reference().child("users").child(uid)
let myPeopleRef = thisUserRef.child("likers")
myPeopleRef.queryLimited(toLast: 30).observeSingleEvent(of: .value, with: { snapshot in
    let userArray = snapshot.children.allObjects as! [DataSnapshot]
    for person in userArray.reversed() where uid == Auth.auth().currentUser?.uid  {
        let personUid = person.value as! String
        self.printPersonInfo(uid: personUid) //// this calls a DispatchQueue.main.async that appends the data in the array and reloads
    }
})

  func printPersonInfo(uid: String) {
        print(uid)
        let usersRef = Database.database().reference().child("users")
        let thisUser = usersRef.child(uid)
        thisUser.observeSingleEvent(of: .value, with: { snapshot in
            let xx = snapshot.childSnapshot(forPath: "xx").value as? String ?? "No Entry"
            let yy = snapshot.childSnapshot(forPath: "yy").value as? String ?? "No Entry"
            let rr = snapshot.childSnapshot(forPath: "rr").value as? String ?? "No Entry"
            let zz = snapshot.childSnapshot(forPath: "zz").value as? String ?? "No Entry"
            let ll = snapshot.childSnapshot(forPath: "ll").value as? String ?? "No Entry"
            let p = Usery(xx: xx, yy: yy, rr: rr, zz: zz, ll: ll)
            self.person.append(p)
            print(self.person, "person")
            self.table.reloadData()
        })
}
//////this gets the various data elements of the users that get displayed. 
///////So to summarize, the query determines which users get displayed. This func determined which data of those users gets displayed.

过去 30 年的数据库整体示例

user K
user B
user F
user G
user K
user B
user K
.....

唯一不起作用的情况是您第一次安装干净的应用程序。那么它显示的顺序是

user K
user K
user B
user B
user F
user G

JSON

users 
  uid 
   likers
      chailbyAutoID: uid1
      chailbyAutoID: uid2
  
4

1 回答 1

0

一种可能的解决方案是使用 DispatchGroups 来控制数据的加载,从而控制填充类 var 数组的顺序。

在此示例中,我们将从用户节点读取所有用户 uid 并填充一个数组。它看起来像这样

users
   uid_0
      name: "Wilbur"
   uid_1:
      name: "Orville"

然后我们将返回并使用 array.reversed() 从该节点重新读取其他用户数据,以便它们以相反的顺序填充结果数组。我们需要一个类来存储每个用户数据,然后需要一个类数组来存储所有读入的用户。这是类 vars

struct PersonClass {
   var uid = ""
   var name = ""
}

var personArray = [PersonClass]()

以及以相反顺序填充它们的代码

func readUserDataUsingDispatch() {
    let ref = self.ref.child("users") //self.ref points to my firebase
    ref.observeSingleEvent(of: .value, with: { snapshot in

        //this just builds and array of user id's - we'll iterate through them to read the rest of the data
        let idArray = snapshot.children.allObjects as! [DataSnapshot]
        var allKeys = [String]()
        for userSnap in idArray {
            allKeys.append( userSnap.key ) //[uid_0, uid_1] etc
        }

        let group = DispatchGroup()

        for uid in allKeys.reversed() {
            group.enter()
            let thisUser = ref.child(uid)

            thisUser.observeSingleEvent(of: .value, with: { userSnapshot in
                let name = userSnapshot.childSnapshot(forPath: "name").value as! String
                let person = PersonClass(uid: uid, name: name)
                self.personArray.append(person)
                print("added uid")
                group.leave()
            })
        }

        group.notify(queue: .main) {
            print("done")
        }
    })
}

当您运行代码时,这是输出,表明数据已加载

added uid
added uid
done

然后在打印类 var 时personArray,它是倒序的用户。

uid_1 Orville
uid_0 Wilbur
于 2021-05-17T17:00:19.930 回答