Ios 儿童计数始终返回0,无法正确到达
我目前正在学习Firebase,并开始编写聊天应用程序 当用户登录和提交名称时我更新Firebase上的值,并且在其他尝试中,用户必须直接到达主页面,但用户一次又一次地进入提交页面 (提交页面=输入用户名) 以下是提交代码:Ios 儿童计数始终返回0,无法正确到达,ios,swift,firebase,firebase-realtime-database,firebase-authentication,Ios,Swift,Firebase,Firebase Realtime Database,Firebase Authentication,我目前正在学习Firebase,并开始编写聊天应用程序 当用户登录和提交名称时我更新Firebase上的值,并且在其他尝试中,用户必须直接到达主页面,但用户一次又一次地进入提交页面 (提交页面=输入用户名) 以下是提交代码: @IBAction func submitButtonAction(sender: AnyObject) { let storageRef = FIRStorage.storage().reference() let databaseRe
@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")
可能这个转换是数据库中的字符串
我还想添加控制台输出
经过一点尝试后解决了问题 //错误
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);
}
输入错误=可选(0x12dsadue12315sdaj123)
正确输入=
0x12dsadue12315sdaj123
不要以图像形式发布代码,而是以文本形式写入。在使用选项进行字符串插值之前,需要先将其展开。在将其传递给updateChildValues
方法之前,请查看调试器中childUpdates
的值,它应该很明显为什么不起作用。然后,它在hanks@dan之前和之后显示0,告诉我错在哪里