简体   繁体   English

queryEqualTo被跳过

[英]queryEqualTo is being skipped over

i'm trying to set up a viewCount for my app, when I set the breakpoints up and go through the code it always skips past the queryOrdered and i'm not exactly sure why 我试图为我的应用程序设置一个viewCount,当我设置断点并通过代码时,它总是跳过了queryOrdered,我不确定为什么

func increaseViewCount(username: String, time: NSNumber){
    guard let uid = Auth.auth().currentUser?.uid else{
        return
    }
    let refOfUserName = Database.database().reference().child("Users").child(uid)
    refOfUserName.observeSingleEvent(of: .value, with: {(snapshot) in
        let dictionaryOfUser = snapshot.value as? [String: AnyObject]
       // let currentUsersName = dictionaryOfUser?["username"] as? String
    let currentUsersName = "hello"

        if username == currentUsersName {
            print("this is the same user")

        }else{

            let postRef = Database.database().reference().child("HistoryOfPosts").child("post")
            postRef.queryOrdered(byChild: "post").queryEqual(toValue: time).observeSingleEvent(of: .childAdded, with: {(snapshotPost) in

                print(snapshotPost.exists())
                print(snapshotPost)

                let valString = snapshotPost.value
                let number = valString as! NSNumber
                var value = number.intValue
                value = value + 1
                let values = ["viewCount": value] as [String:Any]
                postRef.updateChildValues(values)

            })
        }
    })
}

The data is loaded from the Firebase Database asynchronously. 数据是从Firebase数据库异步加载的。 Instead of waiting for that loading to complete, the program continues with the statement after you attach the observer. 附加观察者 ,程序将继续执行该语句,而不是等待该加载完成。

In this case that means that the code pretty much exits increaseViewCount() straight after it attaches the observer. 在这种情况下,这意味着代码在附加观察者后几乎立即退出了gainViewCount increaseViewCount() Then once the data comes back from the Firebase servers, the code in your callback block is executed. 然后,一旦数据从Firebase服务器返回,便会执行回调块中的代码。

To get into the callback block, place a breakpoint on the first statement in that block. 进入回调块,请在该块的第一条语句上放置一个断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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