什么会导致 Swift/Firebase 应用程序在第一次安装时出现问题,然后再也不会出现?

What could cause a Swift/Firebase app to act up the first time it is installed and never again?

什么第一次不行?:

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

我是运行这个

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
  

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

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

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

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

然后在打印 class var personArray 时,用户的顺序相反。

uid_1 Orville
uid_0 Wilbur