Firebase Swift 3 Completion handler Bool

我们两清 提交于 2019-12-06 16:17:54

问题


I'm trying to write a completion handler for a function that checks if a user is a member of a team in firebase.

I have a public class customFunctions in which I created a function ifUserIsMember. I seem to be a little stuck on the idea of completion handlers, and can't seem to figure out how to check the bool value on completion (if that makes sense). Here's my code for the class:

import Foundation
import GeoFire
import FirebaseDatabase


public class customFunctions {

func ifUserIsMember(userid: String, completionHandler: @escaping (Bool) -> ()) {
    let ref = FIRDatabase.database().reference()

    ref.child("teammembers").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.hasChild(userid){
            completionHandler(true)
        }else{
            print("user is not a member of a team")
            completionHandler(false)
        }
    })
}
}

And here is where I'm calling it:

  @IBAction func signInButtonAction(_ sender: AnyObject) {
    //check if user is a member of a team
    let userid = self.uid

    checkFunctions.ifUserIsMember(userid: userid) { success in
        print("user is a member of a team")
        self.updateLocation(type: "in")
    }
}

It seems that it's returning true regardless of whether snapshot.hasChild(uerid) actually has that userid


回答1:


Try using :-

func ifUserIsMember(userid: String, completionHandler: @escaping ((_ exist : Bool) -> Void)) {
    let ref = FIRDatabase.database().reference()

    ref.child("teammembers/\(userid)").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.exists(){
            completionHandler(true)
        }else{
            print("user is not a member of a team")
            completionHandler(false)
        }
    })
}



回答2:


For anybody else that ran into this problem, this is what fixed it for me.

@IBAction func signInButtonAction(_ sender: AnyObject) {
    //check if user is a member of a team

    let userid = self.uid

    checkFunctions.ifUserIsMember(userid: userid) { (exist) -> () in
        if exist == true {
            print("user is a member of a team")
            self.updateLocation(type: "in")
        }
        else {
            print("user is not a member")
        }


    }


}



public class customFunctions {
let ref = FIRDatabase.database().reference()
func ifUserIsMember(userid: String, completionHandler: @escaping ((_ exist : Bool) -> Void)) {

    ref.child("teammembers").observeSingleEvent(of: .value, with: { (snapshot) in
        if snapshot.hasChild(userid){
            completionHandler(true)

        }else{

            print("user is not a member of a team")
            completionHandler(false)
        }


    })

}

}




回答3:


Swift 3 & Firebase 3.17.0

This will do the trick, check for NSNull

func ifUserIsMember(userid: String, completionHandler: @escaping (Bool) -> ()) {
    let ref = FIRDatabase.database().reference()

    ref.child("teammembers").observeSingleEvent(of: .value, with: { (snapshot) in
        guard snapshot.value is NSNull else {
            print("\(snapshot) exists")
            completionHandler(true)
        }
        print("\(snapshot) is not exists")
        completionHandler(false)
    })
}


来源:https://stackoverflow.com/questions/40135276/firebase-swift-3-completion-handler-bool

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!