我正在尝试将用户保存到我的firebase数据库.我正在使用FBSDKLoginManager()来创建帐户/登录.创建帐户后,我想将用户存储到我的firebase数据库中.我现在可以登录用户并且他们的电子邮件显示在firebase的Auth选项卡中(参见屏幕截图),但我的updateChildValues似乎没有任何影响(也见截图).
我将updateChildValues放在正确的位置吗?它目前位于signInWithCredential中.我还必须执行FBSDKGraphRequest来获取我感兴趣的信息存储在我的firebase数据库中.
我的firebase的Auth选项卡显示身份验证正在运行:
但数据库没有更新:
func showLoginView() { let loginManager = FBSDKLoginManager() loginManager.logInWithReadPermissions(fbPermissions,fromViewController: self,handler: { (result:FBSDKLoginManagerLoginResult!,error:NSError!) -> Void in if ((error) != nil) { print("Error loggin in is \(error)") } else if (result.isCancelled) { print("The user cancelled loggin in") } else { // No error,No cancelling: // using the FBAccesstoken,we get a Firebase token let credential = FIRFacebookAuthProvider.credentialWithAccesstoken(FBSDKAccesstoken.currentAccesstoken().tokenString) // using the credentials above,sign in to firebase to create a user session FIRAuth.auth()?.signInWithCredential(credential) { (user,error) in print("User logged in the firebase") // adding a reference to our firebase database let ref = FIRDatabase.database().referenceFromURL("https://project-12345.firebaseio.com/") // guard for user id guard let uid = user?.uid else { return } // create a child reference - uid will let us wrap each users data in a unique user id for later reference let usersReference = ref.child("users").child(uid) // performing the Facebook graph request to get the user data that just logged in so we can assign this stuff to our Firebase database: let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graPHPath: "me",parameters: ["fields": "id,name,email"]) graphRequest.startWithCompletionHandler({ (connection,result,error) -> Void in if ((error) != nil) { // Process error print("Error: \(error)") } else { print("fetched user: \(result)") // Facebook users name: let userName:Nsstring = result.valueForKey("name") as! Nsstring self.usersName = userName print("User Name is: \(userName)") print("self.usersName is \(self.usersName)") // Facebook users email: let userEmail:Nsstring = result.valueForKey("email") as! Nsstring self.usersEmail = userEmail print("User Email is: \(userEmail)") print("self.usersEmail is \(self.usersEmail)") // Facebook users ID: let userID:Nsstring = result.valueForKey("id") as! Nsstring self.usersFacebookID = userID print("Users Facebook ID is: \(userID)") print("self.usersFacebookID is \(self.usersFacebookID)") } }) // set values for assignment in our Firebase database let values = ["name": self.usersName,"email": self.usersEmail,"facebookID": self.usersFacebookID] // update our databse by using the child database reference above called usersReference usersReference.updateChildValues(values,withCompletionBlock: { (err,ref) in // if there's an error in saving to our firebase database if err != nil { print(err) return } // no error,so it means we've saved the user into our firebase database successfully print("Save the user successfully into Firebase database") }) } } }) }
更新:
解决方法
您应该只在执行完成块graphRequest.startWithCompletionHandler时更新值,因为那时您将从Facebook获取数据!
usersReference.updateChildValues需要在graphRequest.startWithCompletionHandler里面({(连接,结果,错误) – >在完成块中无效.我已经在下面附上了它.试试!!
usersReference.updateChildValues需要在graphRequest.startWithCompletionHandler里面({(连接,结果,错误) – >在完成块中无效.我已经在下面附上了它.试试!!
func showLoginView() { let loginManager = FBSDKLoginManager() loginManager.logInWithReadPermissions(fbPermissions,error) -> Void in if ((error) != nil) { // Process error print("Error: \(error)") } else { print("fetched user: \(result)") // Facebook users name: let userName:Nsstring = result.valueForKey("name") as! Nsstring self.usersName = userName print("User Name is: \(userName)") print("self.usersName is \(self.usersName)") // Facebook users email: let userEmail:Nsstring = result.valueForKey("email") as! Nsstring self.usersEmail = userEmail print("User Email is: \(userEmail)") print("self.usersEmail is \(self.usersEmail)") // Facebook users ID: let userID:Nsstring = result.valueForKey("id") as! Nsstring self.usersFacebookID = userID print("Users Facebook ID is: \(userID)") print("self.usersFacebookID is \(self.usersFacebookID)") //graphRequest.startWithCompletionHandler may not come back during serial //execution so you cannot assume that you will have date by the time it gets //to the let values = ["name": //By putting it inside here it makes sure to update the date once it is //returned from the completionHandler // set values for assignment in our Firebase database let values = ["name": self.usersName,"facebookID": self.usersFacebookID] // update our databse by using the child database reference above called usersReference usersReference.updateChildValues(values,ref) in // if there's an error in saving to our firebase database if err != nil { print(err) return } // no error,so it means we've saved the user into our firebase database successfully print("Save the user successfully into Firebase database") }) } }) } } }) }