Junwoo Lee
Junwoo Lee

Reputation: 489

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!

Upvotes: 0

Views: 245

Answers (2)

Atakan Cavuslu
Atakan Cavuslu

Reputation: 954

Try to use your observer code as,

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

}

Upvotes: 2

Dhruv Khatri
Dhruv Khatri

Reputation: 813

Just take self.username = snap.value! as! String It will solve your problem.

Upvotes: 0

Related Questions