小编Jas*_*Jin的帖子

iOS - Firebase观察单个事件调用两次

我对Firebase和iOS开发相对较新,所以我希望有人能给我一些关于我做错的指导.出于某种原因,我的Observe中的代码块正在运行两次,即使ObserveSingleEvent特别应该在读取初始数据后终止观察.

我想要注意的是,这只发生在我的手机上(iPhone 7 iOS10).当我在模拟器中运行它时,我遇到了一个完全不同的问题,我得到了一个应该调用的调用,但是,当我的视图仍在上一个视图中时调用.本质上,应该在主屏幕加载其视图后调用此函数.但是,在模拟器中,当我在登录屏幕中对我的用户进行身份验证时,如果我没有用户名,则会创建弹出窗口,并且实际上不会将下一个视图显示到主屏幕.

这是引起问题的函数:

func initialSetUp() {
    print("initially set up")
    let userRef = FIRDatabase.database().reference(withPath: "/users/").child("\(uid!)")
    userRef.observeSingleEvent(of: .value, with: { (snapshot) in
        print("observing event")
        print("\(snapshot)")

        if snapshot.hasChild("username"){
           print("user has username")
        } else {
            print("user does not have username")
            //do stuff
            let nameRef = FIRDatabase.database().reference(withPath: "/usernames/")
            let alert = SCLAlertView()
            let appearance = SCLAlertView.SCLAppearance(
                kTitleFont: UIFont(name: "Futura-Medium", size: 20)!,
                kTextFont: UIFont(name: "Futura-Medium", size: 14)!,
                kButtonFont: UIFont(name: "Futura-Bold", size: 14)!,
                showCloseButton: false,
                shouldAutoDismiss: false
            )
            alert.appearance = appearance
            let newName = alert.addTextField("Enter …
Run Code Online (Sandbox Code Playgroud)

ios observer-pattern firebase swift firebase-authentication

6
推荐指数
0
解决办法
2083
查看次数