儿童计数总是0,无法正确到达

时间:2016-08-26 20:50:51

标签: ios swift firebase firebase-realtime-database firebase-authentication

我目前正在学习 Firebase 并开始编写聊天应用

当用户登录提交姓名时,我会更新 Firebase 上的值,而在其他尝试用户中,我们必须直接访问主页但用户再次提交页面再次

Firebase Users Child

提交页面 =输入用户名)

这是提交代码:

@IBAction func submitButtonAction(sender: AnyObject) {

        let storageRef = FIRStorage.storage().reference()
        let databaseRef = FIRDatabase.database().reference()


        let uid = FIRAuth.auth()?.currentUser?.uid;


        let profileData = UIImageJPEGRepresentation(UIImage(named: "defaultPhoto")!, 0.4);
        let profileImageRef = storageRef.child("ProfileImages/\(uid)/profileImage.jpg");


        profileImageRef.putData(profileData!, metadata: nil, completion: {
            (metadata , error) in
            if error != nil
            {
                print("error");
            }else
            {
                let username = self.nameTextField.text;
                let user : [NSObject : AnyObject] = ["uid" : uid!,
                    "username":username!];
                let childUpdates = ["Users/\(uid)/":user];
                databaseRef.updateChildValues(childUpdates);


                let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Profiles");
                self.presentViewController(vc!, animated: true, completion: nil);

            }
        })

当用户第一次提交他们的名字(即Jordan,用户名:Blabla)时,它会转到主页面。但在具有相同用户的其他登录中,始终转到提交页面。我无法解决这个问题

然后是登录代码:

func Login(email : String , password : String)
    {
        FIRAuth.auth()?.signInWithEmail(email , password: password, completion:{
            (user , error) in
            if(error != nil)
            {
                print("Somethings wrong")
            }else
            {
                print("User login");

                let uid = FIRAuth.auth()?.currentUser?.uid;

                print("UID == \(uid)");


                let databaseRef = FIRDatabase.database().reference();
                databaseRef.child("Users").child(uid!).observeEventType(.Value, withBlock: {
                    (snapshot) in


                    let userDefaults = NSUserDefaults.standardUserDefaults();
                    userDefaults.setValue(email, forKey: "emailKey");
                    userDefaults.setValue(password, forKey: "passwordKey");



                    if( snapshot.childrenCount == 0)
                    {
                        print("Children count = 0");
                        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Setup");
                        self.presentViewController(vc!, animated: true, completion: nil);

                    }else
                    {
                        print("Children count != 0");
                        let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Profiles");
                        self.presentViewController(vc!, animated: true, completion: nil);


                    }

                })




            }
        });
    }

另外我想知道为什么用户儿童的标题总是带有可选的

Optional("Oa81jsagjassvZb14T1sSnS") 

也许这转换为数据库中的字符串

我也想添加控制台输出

enter image description here

1 个答案:

答案 0 :(得分:2)

经过一番尝试后解决了。

// 错误

  databaseRef.child("Users").child(uid!).observeEventType(.Value, withBlock: {
                (snapshot) in
                print("Children Count ->  \(snapshot.childrenCount) ");
     })
     // child update codes
     databaseRef.updateChildValues(childUpdates);

// 正确

if let testingOptional = uid
{
  databaseRef.child("Users").child(testingOptional).observeEventType(.Value, withBlock: {
            (snapshot) in
            print("Children Count ->  \(snapshot.childrenCount) ");
 })
 // child update codes
 databaseRef.updateChildValues(childUpdates);

}

输入错误= Optional(0x12dsadue12315sdaj123)

正确输入= 0x12dsadue12315sdaj123