Lan*_*ria 8 ios firebase swift firebase-realtime-database
我有两个问题:
我知道如何使用简单的键值对向Firebase添加常规对象,但是如何添加用户对象?
在我的UserAccount对象中,我不确定UserAcct的第二个init方法.我应该使用init(snapshot:FIRDataSnapshot){}添加到Firebase还是应该坚持使用常规init方法?
我的用户模型对象:
import Foundation
import UIKit
import Firebase
import FirebaseDatabase
class UserAccount{
var userID: String
var email: String
var creationDate: String
init(userID: String, email: String, creationDate: String){
self.userID = userID
self.email = email
self.creationDate = creationDate
}//end init
//Is this second init necessary?
init(snapshot: FIRDataSnapshot) {
userID = snapshot.value!["userID"] as! String
email = snapshot.value!["email"] as! String
creationDate = snapshot.value!["creationDate"] as! String
}
}//END class
Run Code Online (Sandbox Code Playgroud)
我的用户签约课程:
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
class CreateAccountController: UIViewController{
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
var dbRef: FIRDatabaseReference!
//Array to hold users
var userAcct = [UserAccount]()
override func viewDidLoad() {
super.viewDidLoad()
//Firebase Ref
self.dbRef = FIRDatabase.database().reference()
}
//Button to sign the user up
@IBAction func signUpButtonPressed(sender: UIButton) {
FIRAuth.auth()?.createUserWithEmail(emailTextField.text!, password: passwordTextField.text!, completion: {
(user, error) in
if error != nil{
print(error?.localizedDescription)
}else{
let emailAddress = self.emailTextField.text!
let currentUserID: String = (FIRAuth.auth()?.currentUser?.uid)!
let accountCreationDate = FIRServerValue.timestamp()
self.userAcct =[UserAccount(userID: currentUserID, email: emailAddress, creationDate: accountCreationDate)]
self.dbRef.child("Users").child("UserID: \(currentUserID)").child("Account-Creation-Date").setValue([\\How to add my self.userAcct model object in here? Should I add it to an array])
}
})
}
Run Code Online (Sandbox Code Playgroud)
这是我用的方法。我在代码上方的注释中解释了所有内容。
\n\n最终结果是您创建一个字典:
\n\nlet dict = [String:Any]()\nRun Code Online (Sandbox Code Playgroud)\n\n然后,您可以使用dictionary\xe2\x80\x99s 方法更新键值对updateValue:
dict.updateValue(someValue, forKey: \xe2\x80\x9csomeKey\xe2\x80\x9d)\nRun Code Online (Sandbox Code Playgroud)\n\n然后你最终将该字典上传到数据库:
\n\nlet userAccountRef = self.dbRef.child("users").child(theUsersID).child(\xe2\x80\x9cuserAccount\xe2\x80\x9d)\n\nuserAccountRef.updateChildValues(dict)\nRun Code Online (Sandbox Code Playgroud)\n\n我的注册用户课程:
\n\nimport UIKit\nimport Firebase\nimport FirebaseAuth\nimport FirebaseDatabase\n\nclass CreateAccountController: UIViewController{\n\n@IBOutlet weak var emailTextField: UITextField!\n@IBOutlet weak var passwordTextField: UITextField!\n\n//Your firebase reference\nvar dbRef: FIRDatabaseReference!\n\n//Current Timestamp in Seconds. You can convert the value later on\nlet timeStamp:NSNumber? = Int(NSDate().timeIntervalSince1970)\n\noverride func viewDidLoad() {\n super.viewDidLoad()\n //Firebase Reference to our database\n self.dbRef = FIRDatabase.database().reference()\n}\n\n//Button to sign the user up\n@IBAction func signUpButtonPressed(sender: UIButton) {\n\n FIRAuth.auth()?.createUserWithEmail(emailTextField.text!, password: passwordTextField.text!, completion: {\n\n //This is where your uid is created. You can access it by using user!uid. Be sure to unwrap it.\n (user, error) in\n print("my userID is \\(user.uid)")\n\n if error != nil{\n print("Account Creation Error: \\(error?.localizedDescription)")\n return\n }\n\n //This constant holds the uid. It comes from the (user, error). The user argument has a uid string property\n let currentUserID = user!.uid // which is the same as FIRAuth.auth()?.currentUser?.uid\n\n //Here you initialize an empty dictionary to hold the keys and values you want uploaded to your database\n let dict = [String:Any]()\n\n //use the dictionary\xe2\x80\x99s updateValue() method to update the values and matching keys\n dict.updateValue(currentUserID, forKey: "userIDKey")\n dict.updateValue(self.emailTextField.text!, forKey: "emailKey")\n dict.updateValue(self.timeStamp!, forKey: "acctCreationDateKey")\n\n //This gives you reference to your database, then to a child node named "users", then another node using the uid, and finally to another node named "userAccount". This final node is where you will keep your dictionary values for your database.\n let userAccountRef = self.dbRef.child("users").child(currentUserID).child(\xe2\x80\x9cuserAccount\xe2\x80\x9d)\n\n //Here you upload your dictionary to the userAccountRef with the dictionary key/values you set above using the dict\xe2\x80\x99s updateValue() method\n userAccountRef.updateChildValues(dict)\n })\n}\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4421 次 |
| 最近记录: |