Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中使用Firebase时返回语句错误_Swift_Firebase_Firebase Realtime Database_Observers - Fatal编程技术网

在Swift中使用Firebase时返回语句错误

在Swift中使用Firebase时返回语句错误,swift,firebase,firebase-realtime-database,observers,Swift,Firebase,Firebase Realtime Database,Observers,我制作了一个函数,它连接到firebase,转到特定路径,然后给我值。当我打印(snapshot.value)时,它会为我提供所需的值。调用该函数时,userProfile为空。我需要userProfile返回快照字符串 func getUserData(uid: String) -> String{ var _REF_USERNAME = FIRDatabase.database().reference().child("users").child(uid).child("pro

我制作了一个函数,它连接到firebase,转到特定路径,然后给我值。当我打印(snapshot.value)时,它会为我提供所需的值。调用该函数时,userProfile为空。我需要userProfile返回快照字符串

 func getUserData(uid: String) -> String{
   var _REF_USERNAME = FIRDatabase.database().reference().child("users").child(uid).child("profile").child("username")

     var userProfile = String()


    _REF_USERNAME.observe(.value, with: {(snapshot) in
        print("SNAP: \(snapshot.value)")
            userProfile = snapshot.value as! String

    })
    print(userProfile)
    return userProfile
}
斯威夫特3

您正在observe中的回调之外调用userProfile,因此它将在observe函数异步完成之前执行。回调一开始很难理解,但一般的想法是,代码不是自上而下顺序运行的,代码的某些部分在后台线程中异步运行

要访问userProfile,必须传入一个回调函数,如下所示:

func observeUserProfile(completed: @escaping (_ userProfile: String?) -> ()) -> (FIRDatabaseReference, FIRDatabaseHandle) {

    let ref = _REF_USERNAME

    let handle = ref.observe(.value, with: {(snapshot) in
            if !snapshot.exists() { // First check if userProfile exists to be safe
                completed(nil)
                return
            }
            userProfile = snapshot.value as! String
            // Return userProfile here in the callback
            completed(userProfile)
    })
    // Good practice to return ref and handle to remove the handle later and save memory
    return ref, handle
let ref, handle = observeUserProfile(completed: { (userProfile) in
     // Get access to userProfile here
     userProfile
}
在传入回调函数时,在外部调用此代码,如下所示:

func observeUserProfile(completed: @escaping (_ userProfile: String?) -> ()) -> (FIRDatabaseReference, FIRDatabaseHandle) {

    let ref = _REF_USERNAME

    let handle = ref.observe(.value, with: {(snapshot) in
            if !snapshot.exists() { // First check if userProfile exists to be safe
                completed(nil)
                return
            }
            userProfile = snapshot.value as! String
            // Return userProfile here in the callback
            completed(userProfile)
    })
    // Good practice to return ref and handle to remove the handle later and save memory
    return ref, handle
let ref, handle = observeUserProfile(completed: { (userProfile) in
     // Get access to userProfile here
     userProfile
}
使用完此观察者后,通过执行以下操作停止其观察以节省内存:

ref.removeObserver(withHandle: handle)
斯威夫特3

您正在observe中的回调之外调用userProfile,因此它将在observe函数异步完成之前执行。回调一开始很难理解,但一般的想法是,代码不是自上而下顺序运行的,代码的某些部分在后台线程中异步运行

要访问userProfile,必须传入一个回调函数,如下所示:

func observeUserProfile(completed: @escaping (_ userProfile: String?) -> ()) -> (FIRDatabaseReference, FIRDatabaseHandle) {

    let ref = _REF_USERNAME

    let handle = ref.observe(.value, with: {(snapshot) in
            if !snapshot.exists() { // First check if userProfile exists to be safe
                completed(nil)
                return
            }
            userProfile = snapshot.value as! String
            // Return userProfile here in the callback
            completed(userProfile)
    })
    // Good practice to return ref and handle to remove the handle later and save memory
    return ref, handle
let ref, handle = observeUserProfile(completed: { (userProfile) in
     // Get access to userProfile here
     userProfile
}
在传入回调函数时,在外部调用此代码,如下所示:

func observeUserProfile(completed: @escaping (_ userProfile: String?) -> ()) -> (FIRDatabaseReference, FIRDatabaseHandle) {

    let ref = _REF_USERNAME

    let handle = ref.observe(.value, with: {(snapshot) in
            if !snapshot.exists() { // First check if userProfile exists to be safe
                completed(nil)
                return
            }
            userProfile = snapshot.value as! String
            // Return userProfile here in the callback
            completed(userProfile)
    })
    // Good practice to return ref and handle to remove the handle later and save memory
    return ref, handle
let ref, handle = observeUserProfile(completed: { (userProfile) in
     // Get access to userProfile here
     userProfile
}
使用完此观察者后,通过执行以下操作停止其观察以节省内存:

ref.removeObserver(withHandle: handle)

在从Firebase返回数据之前执行返回。Firebase数据仅在闭包内部有效。请看我对和的回答。您通常不应该尝试从Firebase函数返回数据,因为它是异步的,而您的其余代码是同步的。返回是在从Firebase返回数据之前执行的。Firebase数据仅在闭包内部有效。请看我对和的回答。通常不应尝试从Firebase函数返回数据,因为它是异步的,而代码的其余部分是同步的。