Junwoo Lee Junwoo Lee - 2 months ago 14
iOS Question

Firebase Database not Uploading

I have my upload code here

import UIKit
import Firebase

class ChatViewController: UIViewController {

let chatRef = FIRDatabase.database().reference().child("chat")
let userUid = FIRAuth.auth()?.currentUser?.uid
var userName = ""
@IBOutlet weak var topBar: UINavigationItem!
@IBOutlet weak var containerView: UIView!
@IBOutlet var inputTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
topBar.title = "Chat Log Controller"
FIRDatabase.database().reference().child("users/\(userUid!)/name").observe(.value) { (snap: FIRDataSnapshot) in
self.userName = (snap.value! as! String).description
}
}

@IBAction func handleSend(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}

@IBAction func handleSendByEnter(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
print(userName)
childChatRef.child("name").setValue(userName)
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}


}

text is successfully uploaded But
It doesn't print userName and doesn't upload it to Firebase Database
But username is nut nil!

Answer

Try to use your observer code as,

ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

}